0

I have been looking at the CSV docs, and have been having some issues with the iteration through rows. If someone could clear it up, that'd be great. For instance, take this code:

custom_feature_string = 'Custom feature 1;custom feature, 2; Custom feature3; custom "feature" 4; customfeature5'
cfeature = StringIO.StringIO(custom_feature_string)
reader = csv.reader(cfeature, delimiter=';', skipinitialspace=True)
for row in reader:
    print '\n'.join(row)

Now this is all good when it comes to printing things out to the screen, but when I try and replace print '\n'.join(row) with print row, a list is printed out containing each entry. I would like to be able to manipulate each entry once as it goes through the iterator. So I would be able to save each entry in a database. Any suggestions on how to do this?

2 Answers 2

1

Your variable names don't accurately reflect what they represent.

reader = csv.reader(cfeature, delimiter=';', skipinitialspace=True)
for row in reader:
    print '\n'.join(row)

row, in your code, is actually a list of all the rows in the CSV. So take a look a look at this:

reader = csv.reader(cfeature, delimiter=';', skipinitialspace=True)
for data in reader:
    for row in data:
        #do something with row

Then you'll be able to process it on a row-by-row basis.

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

3 Comments

I don't think your criticism of the name of row is accurate. Consider: multiline = custom_feature_string + "\n" + custom_feature_string; cfeature = StringIO.StringIO(multiline) and then add print "New line" before the print '\n'.join(row). The output will show two lines.
this worked perfectly, but why am i returning a list? does it always work like this?
@dirtshell: your code doesn't 'return' anything; there's no return statement in sight. However, if you simply do print row after the for row in reader: loop, you'll see that row is a list — which is why it is iterable and can be used in for item in row: (using your code rather than tehsockz's code as the basis for discussion).
1

You can iterate over row:

import StringIO
import csv

custom_feature_string = 'Custom feature 1;custom feature, 2; Custom feature3; custom "feature" 4; customfeature5'
cfeature = StringIO.StringIO(custom_feature_string)
reader = csv.reader(cfeature, delimiter=';', skipinitialspace=True)
for row in reader:
    print '\n'.join(row)
    for item in row:
        print "X", item, "X"

This yields:

Custom feature 1
custom feature, 2
Custom feature3
custom "feature" 4
customfeature5
X Custom feature 1 X
X custom feature, 2 X
X Custom feature3 X
X custom "feature" 4 X
X customfeature5 X

You can manipulate the data in more complex ways than surrounding it with X's if you want, such as insert it into a database. You can manipulate the row as a whole, or in segments, and do the database insertion once per segment, or on a modified version of the row, or otherwise as you please.


A Multi-line Example

import StringIO
import csv

custom_feature_string = 'Custom feature 1;custom feature, 2; Custom feature3; custom "feature" 4; customfeature5'

multiline = custom_feature_string + "\n" + custom_feature_string
cfeature = StringIO.StringIO(multiline)
reader = csv.reader(cfeature, delimiter=';', skipinitialspace=True)
for row in reader:
    print "New line"
    for item in row:
        print "X", item, "X"

Output

New line
X Custom feature 1 X
X custom feature, 2 X
X Custom feature3 X
X custom "feature" 4 X
X customfeature5 X
New line
X Custom feature 1 X
X custom feature, 2 X
X Custom feature3 X
X custom "feature" 4 X
X customfeature5 X

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.