将TXT转换成CSV

import re
import pandas as pd
file = open("C:/Users/zZ/Desktop/evsam2/evaltask2_sample_data/en_sample_data/sample.positive_short.txt","r")
texts = []
for line in file:
    text = []
    index = line.find('>')+1
    index2 = line.find('')
    line = line[index:index2].strip()
    if len(line)==0:
        continue
    text.append(line)
    text.append(1)
    texts.append(text)
print(texts)

name = ['text','polarity']

test = pd.DataFrame(columns=name,data=texts)
test.to_csv('C:/Users/zZ/Desktop/test.csv')
参考:https://jingyan.baidu.com/article/9c69d48ff3123d13c9024e06.html

你可能感兴趣的:(python,CSV)