1

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()
4
  • 1
    What you have tried till now? Commented Aug 12, 2015 at 9:02
  • 2
    I'm voting to close this question as off-topic because SO is not a code writing service, please show your efforts Commented Aug 12, 2015 at 9:02
  • @tec_abhi by what have you tried they are asking code sample Commented Aug 12, 2015 at 9:10
  • sorry guys...please see my sample code above...if instead of ca_final.writerow(column[3]) i give print column[3] i get my output..over screen and i need to take it to other csv file Commented Aug 12, 2015 at 9:12

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

thank you Karan, it worked. I am new to python and didnt realize this. Thanks again for the help...!!
@tec_abhi you could accept his answer by clicking the tick arrow

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.