Ask Your Question
2

CSV Reader

asked 2012-06-20 06:13:03 -0600

Don gravatar image

updated 2012-06-20 06:47:57 -0600

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 “ )”

edit retag flag offensive close merge delete

Comments

Good question

JervisW gravatar imageJervisW ( 2012-06-21 05:22:01 -0600 )edit

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 ( 2012-06-21 16:26:23 -0600 )edit

@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 ( 2012-06-26 04:01:54 -0600 )edit

1 answer

Sort by » oldest newest most voted
1

answered 2012-06-26 03:55:44 -0600

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’, 
 ...]
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

[hide preview]

Question Tools

Stats

Asked: 2012-06-20 06:13:03 -0600

Seen: 517 times

Last updated: Jun 26 '12