一个CSV文件有三列,分别为A B C。现在想用Py导出来也就是一行一个文本,文本用B列当标题,C列当内容。
如图所示:
要做成这样的效果
请问用Python怎么写呢?
网上找到了类似的代码
import os
import xlrd
os.chdir(r"C:\Users\Desktop\shiji\news_data\2013")
def read():
file_path = 'no_data2.csv'
data = xlrd.open_workbook(file_path)
table = data.sheet_by_name('Sheet1')
nrows = table.nrows
for i in range(0, nrows):
file = open('./no_data2/no_data20803_' + str(i) + '.txt', 'wb+')
file.write(str(table.cell(i, 0).value).encode(encoding='utf-8'))
file.close()
return 'done.'
可是运行了错误,由于我的是CSV文件,我把上面的xlrd改为了CSV。不知道怎么弄了,恳请大家指点一下。谢谢。
pandas读取csv到dataframe,然后看你要怎么输出,循环写入文件