splitting delimited strings

John Machin sjmachin at lexicon.net
Wed Jun 15 19:35:11 EDT 2005


Mark Harrison wrote:
> What is the best way to process a text file of delimited strings?
> I've got a file where strings are quoted with at-signs, @like this at .
> At-signs in the string are represented as doubled @@.
> 
> What's the most efficient way to process this?  Failing all
> else I will split the string into characters and use a FSM,
> but it seems that's not very pythonesqe.
> 
> @rv@ 2 @db.locks@ @//depot/hello.txt@ @mh@ @mh@ 1 1 44
> @pv@ 0 @db.changex@ 44 44 @mh@ @mh@ 1118875308 0 @ :@@: :@@@@: @
> 

 >>> import csv
 >>> list(csv.reader(file('at_quotes.txt', 'rb'), delimiter=' ', 
quotechar='@'))
[['rv', '2', 'db.locks', '//depot/hello.txt', 'mh', 'mh', '1', '1', 
'44'], ['pv'
, '0', 'db.changex', '44', '44', 'mh', 'mh', '1118875308', '0', ' :@: 
:@@: ']]
 >>>



More information about the Python-list mailing list