[Tutor] Reading elements in a file

Steve Willoughby steve at alchemy.com
Wed May 4 21:09:30 CEST 2011


On 04-May-11 11:40, Greg Christian wrote:
> Python version = 2.7.1
> Platform = win32
> I am kind of stuck on what is probably a simple thing:
> If we have a file of words like this:
> “first”,”word”,”in”,”that”,”another”,”part”,”of”,”this”

It depends on exactly how "just like" that example the file is.
For example, if it's a comma-separated value (CSV) file, with values 
enclosed optionally in quotes (as shown in your example), you can very 
easily and yet robustly read it using the csv module in the standard 
library.

Otherwise you could use regular expressions to find text inside "" 
quotes and ignore the commas.  Or split on ',' then strip quotes, or...

CSV is easiest if that's a match for your problem domain.

-- 
Steve Willoughby / steve at alchemy.com
"A ship in harbor is safe, but that is not what ships are built for."
PGP Fingerprint 4615 3CCE 0F29 AE6C 8FF4 CA01 73FE 997A 765D 696C


More information about the Tutor mailing list