0

I'm following some feedback from another thread, but have gotten stuck. I'm looking to search an existing csv file to locate the row in which a string occurs. I am then looking to update this row with new data.

What I have so far gives me an "TypeError: unhasable type: 'list'":

allLDR = []
with open('myfile.csv', mode='rb') as f:
    reader = csv.reader(f)
    #allLDR.extend(reader)
    for num, row in enumerate(reader):
        if myField in row[0]:
            rowNum = row

line_to_override = {rowNum:[nMaisonField, entreeField, indiceField, cadastreField]}

with open('myfile.csv', 'wb') as ofile:
    writer = csv.writer(ofile, quoting=csv.QUOTE_NONE, delimiter=',')
    #for line, row in enumerate(allLDR):
    for line, row in enumerate(reader):
        data = line_to_override.get(line, row)
        writer.writerow(data)

1 Answer 1

1

The line allDR.extend(reader) consumes all of the input lines from the csv.reader object. Therefore, the for loop never runs, and rowNum=row is never executed, and {rowNum:blah} generates an exception.

Try commenting out the allDR.extend(reader) line.

As a debugging aid, try adding print statements inside the for loop and inside the conditional.


Here is a program which does what I think you want your program to do: it reads in myfile.csv, modifies rows conditionally based on the content of the first cell, and writes the file back out.

import csv

with open('myfile.csv', mode='rb') as ifile:
    allDR = list(csv.reader(ifile))

for row in allDR:
    if 'fowl' in row[0]:
        row[:] = ['duck', 'duck', 'goose']

with open('myfile.csv', 'wb') as ofile:
    csv.writer(ofile).writerows(allDR)
Sign up to request clarification or add additional context in comments.

9 Comments

hm...just checking--row[0] is the first column, right? that's what i'm trying to read and i'm sure that myField exists in the first column of the csv...i'm going to try your test
See recent edit for a more useful theory. In either event, rowNum=row is never executed.
how do I get around allLDR being empty for the csv writer?
Is the only goal of this program to modify the one line? Do you need allDR for any other reason?
@user25976: You have to rewrite the whole file. That's how text files work. See How to edit a file in place.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.