简体   繁体   中英

Using csv module to write file

I'm trying to extract series numbers (that is the number of bus stops) from a csv file and write in a new csv file. These series numbers usually take the form as follows: "Queen Street, Bus Station - Platform A3 [BT000998]". I only need the content enclosed by the brackets. I found that there are cases that unwanted comma exist (as the example above), and using csv module can avoid such issue. In order to do that I wrote the following code:

import csv
import re
fp = open(r'C:\data\input.csv')
fpw = open(r'C:\data\output.csv','w')
data = csv.reader(fp)
writer = csv.writer(fpw)


for row in data:
    line = ','.join(row)
    lst = line.split(',')
    try:
        stop = lst[11]   # find the location that contains stop number
        extr = re.search(r"\[([A-Za-z0-9_]+)\]", stop)  # extract stop number enclosed by brackets
        stop_id = str(extr.group(1))
        lst[11] = stop_id  # replace the original content with the extracted stop number
        writer.writerow(lst) # write in the output file (fpw)
    except Exception, e:  # this part is in case there is error such as AttributeError
        writer.writerow(row)

After running this code, while there is no error raised, only an empty csv file is generated. I'm quite new to python. Much appreciate if anyone can help me with this code to make it work.

Thank you in advance. Sui

====UPDATE====

Based on everyone's reply, I revised the code as follows:

import csv
import re
fp = r'C:\data\input.csv'
fpw = r'C:\data\output.csv'

with open(fp, 'rb') as input, open(fpw, 'wb') as output:
               for row in csv.reader(input):
                   try:
                       stop = row[11]
                       extr = re.search(r"\[([A-Za-z0-9_]+)\]", stop)
                       stop_id = str(extr.group(1))
                       row[11] = stop_id
                       repl_row = ','.join(row) + '\n'
                       output.write(repl_row)
                   except csv.Error:
                       pass

Now running the code seems working. HOWEVER, in the middle of running, an error 'line contains NULL byte' was raised, and python stopped even though I added try/except as shown above. So anyone has suggestion to deal with this issue and the let the code continue? By the way, the csv file I'm working on is over 2GB.

Many thanks, Sui

If that's the whole code, you need to close the file with fpw.close() after you are done with all the writer operations.

You can also try with keyword, as in official Python documentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM