1

I am fairly new to Python and am working with the NLTK to produce a sound dynamic Text Analyzer. I have a .csv file with member information, survey response number, and survey response text that I need open and read.

I have:

import csv
import codecs

f = open('testresponseFS.csv')
raw = f.read()
print raw

This may be a bit over my head, but I want to read each row in the file to keep all information intact, and read a specific cell "response" which contains text response. I have been suggested put that specific column in an array, iterating through the whole column with array values; therefore I can run functions on each item in that array, and eventually append those values back to the .csv file next to the "response".

1 Answer 1

3
import csv

# read data
with open('testresponseFS.csv', 'rb') as inf:
    incsv = csv.reader(inf)
    header = next(incsv)
    data = [row for row in incsv]

# process data
header.append('Comments')
response_column = 4
for row in data:
    response = row[response_column]
    newval = response[:4].lower()    # or whatever you do to it
    row.append(newval)   

# write data back out
with open('finaldata.csv', 'wb') as outf:
    outcsv = csv.writer(outf)
    outcsv.writerow(header)
    outcsv.writerows(data)
Sign up to request clarification or add additional context in comments.

Comments

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.