gpt4 book ai didi

python - 使用python代码从csv文件中提取

转载 作者:太空宇宙 更新时间:2023-11-03 17:30:12 25 4
gpt4 key购买 nike

我有一个 csv 文件,其中包含从某些推特 ID 中提取的推文。我需要删除原始推文文本之前的前 3 列。例如

ArvindKejriwal,630345258765697024,2015-08-09 11:49:55,"RT @NitishKumar: No better place to start than from the land of Budhha. We commit no tickets to criminals. Now,show courage & commit to thi…"

我只想提取“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()

最佳答案

您必须先关闭文件对象,然后才能再次打开它。

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()

column[3:] 将跳过前三列。

关于python - 使用python代码从csv文件中提取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31960498/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com