I have a text file with a list of strings.
I want to search a .csv file for rows that begin with those strings and put them in a new .csv file.
In this instance, the text file is called 'output.txt', the original .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
The error:
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
Problem was that the CSV file was saved with tabs instead of commas, new issue now is the following:
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 has 500+ entries of data... does this make a difference?