Python-替换CSV文件中的行的值

Python-替换CSV文件中的行的值,python,csv,Python,Csv,我有以下数据集: ['XXXX-XXXX', '0'] ['XXXX-XXXX', '0'] ['XXXX-XXXX', '0'] ['XXXX-XXXX', '0'] ['XXXX-XXXX', '0'] ['XXXX-XXXX', '0'] ['XXXX-XXXX', '0'] ['XXXX-XXXX', '0'] 基本上,我希望在每次运行程序后,以增量方式将第二个字段“0”更改为“1”,如下所示: ['XXXX-XXXX', '1'] # first run ['XXXX-XXXX',

我有以下数据集:

['XXXX-XXXX', '0']
['XXXX-XXXX', '0']
['XXXX-XXXX', '0']
['XXXX-XXXX', '0']
['XXXX-XXXX', '0']
['XXXX-XXXX', '0']
['XXXX-XXXX', '0']
['XXXX-XXXX', '0']
基本上,我希望在每次运行程序后,以增量方式将第二个字段“0”更改为“1”,如下所示:

['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']

应直接编辑.csv文件。我根本不知道如何解决这个问题,我是一个python新手。

这里有一些东西可以让您朝着正确的方向前进

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']
对于小文件,可以执行以下操作:

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)
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')
对于
inf.readlines
将终止内存分配的大型文件,因为它会立即将整个文件拉入内存,您应该执行以下操作:

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)
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')

找到第一个0,更改为1,然后只写其余的行。如果您根本不知道如何处理问题,那么您在此处询问时没有正确使用堆栈溢出。这是为你在中途遇到问题而准备的。你才刚刚开始,这可能会对你有所帮助