First time here? We are a friendly community of Power Systems Engineers. Check out the FAQ!

Ask Your Question
2

CSV Reader

asked Jun 20 '12

Don gravatar image

updated Jun 20 '12

JervisW gravatar image

I have been playing with the csv.reader module in reading a column in the following format.

[‘filename1’,
 filename2’,
 ……….. 
]

However, I end up with getting an extra-bracket which I don’t want

[ ( filename1’,
    filename2’, 
   ………….)
]

Just wondering whether you want to suggest me a pythonic way of getting rid of the “ ( “ and “ )”

Comments

Good question

JervisW gravatar imageJervisW (Jun 21 '12)

Can you share a bit more information? csv.reader.next() returns a list of values each row like [col1, col2, col3]. I'm not sure how you're getting [ (col1, col2, col3) ]

chip gravatar imagechip (Jun 21 '12)

@chip. If you do something like list(reader) it will create a list of tuples, rather than returning a single row. I suspect that is what is going on here.

JervisW gravatar imageJervisW (Jun 26 '12)

1 answer

Sort by » oldest newest most voted
1

answered Jun 26 '12

Daniel_Hillier gravatar image

Your list has a tuple as its first element. To grab the tuple, just you need to get the 0-th element from the list:

>>> data = [(‘filename1’,
...         filename2’, 
...          ...)]
>>> filenames = data[0]
>>> print filenames
(‘filename1’,
 filename2’, 
 ...)

As tuples can't be altered once they have been created, if you want to change the data in the tuple, you will need to convert it to a list first:

>>> filenames = list(filenames)
>>> print filenames
[‘filename1’,
 filename2’, 
 ...]
link

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.
Want to format code in your answer? Here is a one minute demo on Youtube

Add Answer

[hide preview]

Question Tools

Stats

Asked: Jun 20 '12

Seen: 519 times

Last updated: Jun 26 '12