3

I have this dataset:

['XXXX-XXXX', '0']
['XXXX-XXXX', '0']
['XXXX-XXXX', '0']
['XXXX-XXXX', '0']
['XXXX-XXXX', '0']
['XXXX-XXXX', '0']
['XXXX-XXXX', '0']
['XXXX-XXXX', '0']

Basically I want to incrementally change the second field '0' to '1' after each run of the program, like this:

['XXXX-XXXX', '1'] # first run
['XXXX-XXXX', '0']
['XXXX-XXXX', '0']
['XXXX-XXXX', '0']
['XXXX-XXXX', '0']
['XXXX-XXXX', '0']
['XXXX-XXXX', '0']
['XXXX-XXXX', '0']

['XXXX-XXXX', '1'] # second run
['XXXX-XXXX', '1']
['XXXX-XXXX', '0']
['XXXX-XXXX', '0']
['XXXX-XXXX', '0']
['XXXX-XXXX', '0']
['XXXX-XXXX', '0']
['XXXX-XXXX', '0']

['XXXX-XXXX', '1'] # eigth run
['XXXX-XXXX', '1']
['XXXX-XXXX', '1']
['XXXX-XXXX', '1']
['XXXX-XXXX', '1']
['XXXX-XXXX', '1']
['XXXX-XXXX', '1']
['XXXX-XXXX', '1']

The .csv file should be edited directly. I haven't the slightest idea on how to approach this problem, I'm a python novice..

Carp Sagan
  • 63
  • 1
  • 1
  • 3
  • find the first 0, change to a 1 then just write the rest of the lines – Padraic Cunningham Feb 09 '15 at 18:30
  • 2
    If you haven't the slightest idea how to approach a problem, then you are not using Stack Overflow correctly by asking here. This is for when you are midway through a problem and got stuck. You are at the very beginning. – Two-Bit Alchemist Feb 09 '15 at 18:30
  • this might help you http://stackoverflow.com/questions/11033590/change-specific-value-in-csv-file-via-python – Daniel Feb 09 '15 at 18:40

1 Answers1

9

Here's something to get you going in the right direction.

with open('path/to/filename') as filehandler_name:
    # this is how you open a file for reading

with open('path/to/filename', 'w') as filehandler_name:
    # this is how you open a file for (over)writing
    # note the 'w' argument to the open built-in

import csv
# this is the module that handles csv files

reader = csv.reader(filehandler_name)
# this is how you create a csv.reader object
writer = csv.writer(filehandler_name)
# this is how you create a csv.writer object

for line in reader:
    # this is how you read a csv.reader object line by line
    # each line is effectively a list of the fields in that line
    # of the file.
    # # XXXX-XXXX, 0 --> ['XXXX-XXXX', '0']

For small files, you could do something like:

import csv

with open('path/to/filename') as inf:
    reader = csv.reader(inf.readlines())

with open('path/to/filename', 'w') as outf:
    writer = csv.writer(outf)
    for line in reader:
        if line[1] == '0':
            writer.writerow(line[0], '1')
            break
        else:
            writer.writerow(line)
    writer.writerows(reader)

For large files that inf.readlines will kill your memory allocation since it's pulling the whole file into memory at once, and you should do something like:

import csv, os

with open('path/to/filename') as inf, open('path/to/filename_temp', 'w') as outf:
    reader = csv.reader(inf)
    writer = csv.writer(outf)
    for line in reader:
        if line[1] == '0':
           ...
        ... # as above

os.remove('path/to/filename')
os.rename('path/to/filename_temp', 'path/to/filename')
somesingsomsing
  • 3,182
  • 4
  • 29
  • 46
Adam Smith
  • 52,157
  • 12
  • 73
  • 112