0

I have a list of addresses and latitude,longitudes which I am printing from a text file. It has a few 0's so I am using try and except blocks.

Now I want to save the output to a CSV file and I am trying this code:

with open("qwe.txt",'r') as fp, open("qwert.txt",'w') as fr:
    for line in fp:
        #try:
            g= geocoder.google(line)
            #print(g.address,g.latlng)
       # except:
            #print(0,0)
            writer = csv.writer(fr, delimiter=',')
            writer.writerows(zip(g.address,g.latlng))

fp.close()

but the loop is exiting when there is 0 occurence. I want to write try and catch on a single line so that I can print the output to a CSV.

I tried using lambda but looks like lamda doesn't work for try and except.

jdgregson
  • 1,457
  • 17
  • 39
san guine
  • 49
  • 2
  • 5
  • 1
    Your title hardly seems to match your question. Which one is the real question? – jdgregson Oct 06 '17 at 05:58
  • hello @jdgregson sorry for the ambiguity, I have edited the post and its purpose now – san guine Oct 06 '17 at 06:04
  • 2
    Possible duplicate of [Python: try statement in a single line](https://stackoverflow.com/questions/2524853/python-try-statement-in-a-single-line) – jdgregson Oct 06 '17 at 06:08
  • I could write it in a different way. I wrote a loop to print it to csv inside try block and left except to print 0's. In my case, I dont want to have 0's in my csv. This worked for me in my case. Thanks – san guine Oct 06 '17 at 06:10

1 Answers1

3

Please refer to this SO question: Python: try statement in a single line

There is no way to compress a try/except block onto a single line in Python.

jdgregson
  • 1,457
  • 17
  • 39