First, because it irks the hell out of me when I ask a question and I get the reply, "You don't want to do that, do this", here's the simple answer to your question...
Any string is of the python class 'String', and therefore has all the methods of String, so you can take the empty string, '', and use its join() method to concatenate list elements:
Code:
>>> str = 'ABCD'
>>> lst = list( str )
>>> print lst
['A', 'B', 'C', 'D']
>>> print ''.join( lst )
ABCD
>>> print ','.join( lst )
A,B,C,D
However, converting a string to a list is redudant, because a String is a special case of a list:
Code:
>>> for char in str:
... print char
...
A
B
C
D
So you can skip that step.
Also, if you look at the string methods and regexp methods you can probably find something that would be more efficient.
Code:
>>> str = '"ABCD"'
>>> print str.replace( '"', '' );
ABCD
>>> print ''.join( str.split( '"' ) )
ABCD
(Both of these examples eliminate *all* quotes, not just enclosing)
It's not just about not reinventing the wheel, but when you can replace a loop with a map() or a method, those are (usually) implemented in C and will be much faster at runtime. Probably not important for a one off script, but it's good to learn the tools available.
And, finally (will this guy ever shut up?!?), since we're talking about knowing your tools, if you're using python 2.3, it comes with a built in csv parsing module. You instantiate a reader with an iterable (in your case, open your file and pass the file handle to the reader() method, in the case of the example, I'm going to use a list with one item in it) and iterate over the elements, the default parser is pretty intelligent:
Code:
>>> import csv
>>> # you would use: iterator = open( "list.txt" )
>>> iterator = [ '1,two,3,"I think, therefore I am",5,6', ]
>>> p = csv.reader( iterator )
>>> for thing in p:
... print thing
...
['1', 'two', '3', 'I think, therefore I am', '5', '6']
Notice that it properly handled the numbers, the unquoted string and the quoted string with an embedded delimiter. All in five lines of code.
Python Rocks.
