I have a text file with a list of strings.
I want to find the .csv file for lines starting with these lines and put them in a new CSV file.
In this case, the text file is called "output.txt", the source .csv is "input.csv", and the new .csv file is "corrected.csv".
The code:
import csv file = open('output.txt') while 1: line = file.readline() writer = csv.writer(open('corrected.csv','wb'), dialect = 'excel') for row in csv.reader('input.csv'): if not row[0].startswith(line): writer.writerow(row) writer.close() if not line: break pass
Mistake:
Traceback (most recent call last): File "C:\Python32\Sample Program\csvParser.py", line 9, in <module> writer.writerow(row) TypeError: 'str' does not support the buffer interface`
New error:
Traceback (most recent call last): File "C:\Python32\Sample Program\csvParser.py", line 12, in <module> for row in reader: _csv.Error: line contains NULL byte
The problem was that the CSV file was saved with tabs instead of commas, now a new problem:
Traceback (most recent call last): File "C:\Python32\Sample Program\csvParser.py", line 13, in <module> if row[0] not in lines: IndexError: list index out of range
The CSV file contains more than 500 data records ... does it matter?
python csv
James Roseman Oct 21 '11 at 18:20 2011-10-21 18:20
source share