输入文本:
May 2014, 156
May 2013, 556
May 2013, 651
我将此数据添加到input.csv
文件中:
import csv
with open("text_file.txt") as inputFile:
for line in inputFile:
vars = [line.strip().split(",") for line in inputFile]
print vars
#set True if you want to export input as csv file
convert_to_csv = True
if convert_to_csv == True:
with open('input.csv', 'w') as fp:
a = csv.writer(fp, delimiter=',')
data = [['Text', 'Count']] + vars
a.writerows(data)
如何从我的变量中获取变量vars
并将 10(例如:156 + 10)添加到我的“计数”值?
打印变量输出:
[['May 2013', 156], ['May 2013', 556]]
计算后计数 = 计数 + 10,我也想将输出写入带有标题的 csv 文件(output.csv)['Text', 'Count after calc']