0

In pseudocode: I need a block of code that can access a .csv file, go to the nth row, with n being a predetermined random number between 1, 100, go to the 3rd column of nth row, and replace the 'FALSE' string there with a string that reads 'TRUE'.

Here's what SO has told me so far: I might need to read the file, write over it to a new file, and then rename back to the original file name(?) for this to work.

Here's the code I have thus far:

rnum = randint(1,100)
file1 = open('file.csv', 'rb') 
reader = csv.reader(file1)
file2 = open('file11.csv', 'wb')
writer = csv.writer(file2)
for i, row in enumerate(reader):
    if i == rnum:
        row[2] = 'TRUE'
writer.writerow(row)
file1.close()
file2.close()

The code doesn't work -- in the file1.csv file, all that is printed is the last row of the original .csv file, which isn't what I want.

I suppose ideally the whole file.csv file should be transferred to the file1.csv, but with the nth row modified as described above. Thoughts?

Sorry if this is an obvious question, I just started programming.

Related SO questions on this topic:
Change specific value in CSV file via Python
Writing to a particular cell using csv module in python - this question suggests creating a function for this issue?
read and write on same csv file

8-Bit Borges
  • 9,643
  • 29
  • 101
  • 198
zthomas.nc
  • 3,689
  • 8
  • 35
  • 49

2 Answers2

1

You're almost there. Notice what you're doing in the loop and what you're writing. you have this:

for i, row in enumerate(reader):
    if i == rnum:
        row[2] = 'TRUE'
writer.writerow(row)

I believe what you want to do is enumerate over the rows of the file and copying them over to the file you're writing to. You don't actually write anything except the last row enumerated over since your writes are not within the loop, it's outside. Make sure it's part of the loop.

for i, row in enumerate(reader):
    if i == rnum:
        row[2] = 'TRUE'
    writer.writerow(row)

On a side note, it would be better to open your files using the with statement. It will help manage the files for you so you don't have to close it at the end, it will be taken care of for you.

rnum = randint(1,100)
with open('file.csv', 'rb') as file1, open('file11.csv', 'wb') as file2:
    reader = csv.reader(file1)
    writer = csv.writer(file2)
    for i, row in enumerate(reader):
        if i == rnum:
            row[2] = 'TRUE'
        writer.writerow(row)
Jeff Mercado
  • 129,526
  • 32
  • 251
  • 272
  • Terrific, thanks a bunch. You are a life-saver. Do you have an insight on how you might modify your given code so that I could read and write to the same file? – zthomas.nc Aug 06 '14 at 22:54
  • Unless the files you're processing are relatively small, I'd say it isn't worth it. It will always be easier to read and write to separate files. Basically if you wanted to read/write to the same file, you would need to read everything into memory, process it, then write that back to the file. And if you're dealing with massive amounts of data, it could eat up all that you have. – Jeff Mercado Aug 06 '14 at 23:28
  • The .csv file in question is 6KB ... I'm guessing that wouldn't eat up all my memory? – zthomas.nc Aug 07 '14 at 00:21
  • 1
    So to make the appropriate changes to your code, rather than writing the rows back to a file when you loop through it, just put the rows into a list first. After you have read the entire file and processed it, loop over that list again and write the rows back to the file again. – Jeff Mercado Aug 07 '14 at 00:33
0

The best way to do this would be to only have one file. It will save you wasting memory and worrying about how to change a file name. If you open file with the write command the files contents will be emptied.

I've wrote this quickly now I cant guarantee that it works but its along the right lines.

import csv
def OpenFile(fname):
    with open(fname,'r') as f:
        reader = csv.reader(f)
        file = []
        for row in reader:
            file.append(row)
    return file

def WriteFile(fname,data):
    with open(fname,'w') as f:
        writer = csv.writer(f)
        for row in data:
            writer.writerow(row)
rnum = randint(100)
file = OpenFile('file.csv')
file[rnum][2] = 'TRUE'
WriteFile(fname,file)
Riley
  • 4,122
  • 3
  • 16
  • 30