读取csv文件,逐行写入txt

import csv    #加载csv包便于读取csv文件

x, y = [], []
csv_file = open('G:/竞赛/datafountain/O2O商铺食品安全相关评论发现/rnn_cnn/data/train.csv',encoding='utf-8')    #打开csv文件
next(csv_file)
csv_reader_lines = csv.reader(csv_file)   #逐行读取csv文件
for one_line in csv_reader_lines:
    label, content = one_line[0].strip().split('\t')
    x.append(content)
    y.append(label)
with open('G:/竞赛/datafountain/O2O商铺食品安全相关评论发现/rnn_cnn/data/train_x.txt',encoding='utf-8',mode='w') as f:
    for i in x:
        f.write('{}\n'.format(i))
with open('G:/竞赛/datafountain/O2O商铺食品安全相关评论发现/rnn_cnn/data/train_y.txt',encoding='utf-8',mode='w') as f:
    for i in y:
        f.write('{}\n'.format(i))

你可能感兴趣的:(Python)