[Tutor] Coma separated instead TAB separated
Mario Py
mariopy at gmx.com
Mon Jun 9 02:03:39 CEST 2014
Thank you Alan, so simple solution! Just show how little I know!
>> You should probably use the csv module instead <<
For now split should be OK, in the future I will try to learn CVS module.
OK I understand, new subject, new post, thanks again
Mario
On 6/8/2014 5:33 PM, Alan Gauld wrote:
> On 08/06/14 19:56, Mario Py wrote:
>> Hi everyone, this is very basic/beginner question.
>
> Hi,
>
> Please don;t reply to an existing message to start a new discussion. It
> messes up the threading and makes it harder for people searching the
> archives in the future.
>
>> I'm reading TXT file, two words per line that are separated by TAB:
>>
>> question, rightAnswer = line.strip().split('\t')
>>
>> I would like to use TXT file that it would be separated by coma.
>> How do I change that line of code?
>
> You should probably use the csv module instead, it is designed for
> reading comma separated files (or tab or any other character).
> But if you really do only have two fields and you know they won't have
> commas within them then you can do it with split()...
>
>> I tried these two versions but it is not working:
>>
>> question, rightAnswer = line.strip().split('\c') # c for coma?
>> question, rightAnswer = line.strip().split('\,') # , for coma?
>
> Since comma is not a special character you don't need the escape(\)
> character. So your line should just be:
>
> question, rightAnswer = line.strip().split(',')
>
> HTH
More information about the Tutor
mailing list