如果只有一列数据,假设数据如你所贴出的,代码如下:import csv
with open('data2.csv', 'r', encoding='utf-8') as csvfile:
reader = csv.reader(csvfile)
data = list(reader)
with open('result2.csv', 'w', encoding='utf-8', newline='') as csvfile:
writer = csv.writer(csvfile)
for line in data:
if int(line[0]) > 20000:
line.append('是')
else:
line.append('否')
writer.writerow(line)
假如数据有多列,分割符为逗号,假设数据如下(其他数据用数字代替了):8717,0,1
9506,0,1
10666,0,1
12487,0,1
14368,0,1
16738,0,1
20494,0,1
24100,0,1
26180,0,1
30808,0,1
36277,0,1
39771,0,1
43497,0,1
46912,0,1
49922,0,1
53783,0,1
59592,0,1
65534,0,1
70328,0,1
72000,0,1
那么代码如下:import csv
with open('data.csv', 'r', encoding='utf-8') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
data = list(reader)
with open('result.csv', 'w', encoding='utf-8', newline='') as csvfile:
writer = csv.writer(csvfile)
for line in data:
if int(line[0]) > 20000:
line.append('是')
else:
line.append('否')
writer.writerow(line)
|