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 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?
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.