Reputation: 85
i have a csv file which contains extracted tweets from some tweeter id. i need to get rid of first 3 columns i get before the original tweets text. e.g.
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…"
I just want to extact text after "RT...." and store in another csv file. Please suggest...i have this in bunch say some 2k rows. how to achieve this?
my sample code:
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()
Upvotes: 1
Views: 104
Reputation: 300
You have to close the file objects before opening it again.
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:] will skip first three columns.
Upvotes: 1