我有一个csv文件,其中包含来自某个tweeter id的提取推文。我需要摆脱原始推文文本之前的前3列。 e.g。
ArvindKejriwal,630345258765697024,2015-08-09 11:49:55," RT @NitishKumar:没有比Budhha更好的起点了。我们没有犯罪分子的门票。现在,表现出勇气和勇气承诺......"
我只想在" RT ...."之后取消文字。并存储在另一个csv文件中。请建议......我有一堆说2k行。如何实现这个目标?
我的示例代码:
import csv
inputCSV = open(r'C:\\...\\ArvindKejriwal_tweets.csv', 'rb')
outputCSV = open(r'C:\\...\\\\OUTPUT.csv', 'wb')
appendCSV = open(r'C:\\...\\\\OUTPUT.csv', 'ab')
appendCSV11 = open(r'C:\\...\\\\OUTPUT_Final.csv', 'ab')
cr = csv.reader(inputCSV, dialect = 'excel')
cw = csv.writer(outputCSV, dialect = 'excel')
ca = csv.writer(appendCSV, dialect = 'excel')
ca_final=csv.writer(appendCSV11, dialect='excel')
for row in cr:
if row or any(row) or any(field.strip() for field in row):
ca.writerow(row)
f=csv.reader(open('C:\\..\\OUTPUT.csv','rb'))
for column in f:
if column or any(column) or any(fields.strip() for fields in column):
ca_final.writerow(column[3])
# close files
inputCSV.close()
outputCSV.close()
appendCSV.close()
答案 0 :(得分:1)
您必须先关闭文件对象,然后再将其打开。
for row in cr:
if row or any(row) or any(field.strip() for field in row):
ca.writerow(row)
# add these two lines
outputCSV.close()
appendCSV.close()
f=csv.reader(open('C:\\..\\OUTPUT.csv','rb'))
for column in f:
if column or any(column) or any(fields.strip() for fields in column):
ca_final.writerow(column[3:]) # put a colon here
# close files
inputCSV.close()
outputCSV.close()
appendCSV.close()
列[3:]将跳过前三列。