Question

In: Computer Science

fp=open("us-counties.2.txt","r") #open file for reading fout=open('Linsey.Prichard.County.Seats.Manipulated.txt','w') #file for writting states=[i.strip() for i in fp.readlines()] #read the...

fp=open("us-counties.2.txt","r") #open file for reading
fout=open('Linsey.Prichard.County.Seats.Manipulated.txt','w') #file for writting
states=[i.strip() for i in fp.readlines()] #read the lines
try:
#aplit values and write into file
a,b=state.split(',')
print(a,b)
#write into file
fout.write(a+":"+b+"\n")\
except Exception#if we dont have two names (For a line like KENTUCKY or OHIO, It continues)
Pass

evaluate Data. Manipulation.py]
Traceback (most recent call last):
File "C:/Users/Prichard/Data. Manipulation.py", line 10, in <module>
except Exception#if we dont have two names (For a line like KENTUCKY or OHIO, It continues)
Syntax Error: invalid syntax: <string>, line 10, pos 9

Can someone tell me why I am getting this error?

Solutions

Expert Solution

From the error it seems that you missed out the syntax of line 10, which is except block. This error will raise when you miss proper indendation or : .There is no colon used after except keyword. If you want to take up the exception, you can use "except Exception as e:" something like that. I will rewrite the code for you.

fp=open("us-counties.2.txt","r") #open file for reading
fout=open('Linsey.Prichard.County.Seats.Manipulated.txt','w') #file for writting
states=[i.strip() for i in fp.readlines()] #read the lines
try:
  #split values and write into file
  a,b=state.split(',')
  print(a,b)
  #write into file
  fout.write(a+":"+b+"\n")
except Exception as e: #You can use "except:" also#if we dont have two names (For a line like KENTUCKY or OHIO, It continues)
  pass

Follow the above code. It will definitely work for you.

#Please dont forget to upvote if you like my work. This will help me to provide better solutions with great effort.


Related Solutions

#Java I am reading from the txt file and I put everytihng inside of the String[],...
#Java I am reading from the txt file and I put everytihng inside of the String[], like that: String[] values = str.split(", "); But, now I have to retrieve the data from it one by one. How can I do it? Here is the txt file: OneTime, finish the project, 2020-10-15 Monthly, wash the car, 2020-11-10 Thanks!
APPLIED STATISTICS 2 USE R CODE! SHOW R CODE Use data file RecordMath2526.txt, to produce a...
APPLIED STATISTICS 2 USE R CODE! SHOW R CODE Use data file RecordMath2526.txt, to produce a plot graph with Exam1 as x, Exam2 as y, use Gender as color, and Hw1 as pch. RecordMath2526 information Index Gender Hw1 Hw2 Hw3 Exam1 Hw4 Exam2 Hw5 Hw6 Hw7 Final 1 F 9 6 8 60 7 82 10 10 9 69 2 M 10 10 10 94 9 98 10 10 8 91 3 M 9 10 8 79 9 55 10...
I would need both SAS & R code, the output and .txt file/s, please. Because many...
I would need both SAS & R code, the output and .txt file/s, please. Because many HMOs either do not cover mental health costs or provide only minimal coverage, min- isters and priests often need to provide counseling to persons suffering from mental illness. An in- terdenominational organization wanted to determine whether the clerics from different religions have different levels of awareness with respect to the causes of mental illness. Fifteen clerics from different Christian denominations were sampled. Each was...
R studio. The file I read contains the lower and upper bound of 10,000 98% confidence...
R studio. The file I read contains the lower and upper bound of 10,000 98% confidence intervals for a population mean. All intervals are constructed from samples of size 30 from the same population. As a comment, how many of the 10,000 intervals do we expect to contain the true population mean? Use if else function to determine if the true population mean of 50 is contained in each interval. If an interval contains the true population mean , then...
Scenario # 5 Seniors-R-Us Nursing homes plan to open 2 facilities in a community with a...
Scenario # 5 Seniors-R-Us Nursing homes plan to open 2 facilities in a community with a large population of baby boomers. The city government will not approve building permits until a plan is submitted that will show how the Seniors-R-Us Nursing Homes will benefit the community and partner with present HCF/Health Care Facilities to provide service. Submit a plan to the city. Describe your facility Describe your staff Explain any legal or ethical issues of the situation Explain your management...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT