Python 为什么不插入?

Python 为什么不插入?,python,csv,insert,Python,Csv,Insert,我有一个包含多列的csv文件,如果它为空,我想将一列的内容添加到相邻的列中 import csv with open("new.csv", "r+b") as f: reader = csv.reader(f) for rows in reader: if rows[4] == "": rows.insert(5,rows[3]) 我在脚本中做的事情是,我将所有偶数替换为0,但您可以简单地更改它们。读者不会写。。查看此帖子以供参考; with open('i.csv',

我有一个包含多列的csv文件,如果它为空,我想将一列的内容添加到相邻的列中

import csv
with open("new.csv", "r+b") as f:
reader = csv.reader(f)
for rows in reader:
    if rows[4] == "":
        rows.insert(5,rows[3])

我在脚本中做的事情是,我将所有偶数替换为0,但您可以简单地更改它们。

读者不会写。。查看此帖子以供参考;
with open('i.csv', 'r') as f:
    reader = csv.reader(f)
    my_list = []
    for rows in reader:
        sub_list = []
        for item in rows:
            # Replace even values by 0
            # This can be changed to do something else
            if int(item) % 2 == 0:
                item = 0
            sub_list.append(item)
        my_list.append(sub_list)       

print my_list
# output
#[['1', 0, '3', 0],
#[0, '3', 0, '5'],
#['5', 0, '7', 0],
#['9', 0, '11', 0],
#['13', 0, '15', 0]]

# Write the modified content to the same file
with open('i.csv', 'w') as f:
    writer = csv.writer(f)
    for row in my_list:
        writer.writerow(row)




# Read it to see if it worked
with open('i.csv', 'r') as f:
    for line in f.readlines():
        print line

# output
# 1,0,3,0
# 0,3,0,5
# 5,0,7,0
# 9,0,11,0
# 13,0,15,0