Reading files, splitting on a delimiter and newlines.

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Mon Jul 23 01:17:21 CEST 2007


attn.steven.kuo at gmail.com a écrit :
> On Jul 25, 8:46 am, chris... at gmail.com wrote:
> 
>>Hello,
>>
>>I have a situation where I have a file that contains text similar to:
>>
>>myValue1 = contents of value1
>>myValue2 = contents of value2 but
>>                        with a new line here
>>myValue3 = contents of value3
>>
>>My first approach was to open the file, use readlines to split the
>>lines on the "=" delimiter into a key/value pair (to be stored in a
>>dict).
>>
>>After processing a couple files I noticed its possible that a newline
>>can be present in the value as shown in myValue2.
>>
>>In this case its not an option to say remove the newlines if its a
>>"multi line" value as the value data needs to stay intact.
>>
>>I'm a bit confused as how to go about getting this to work.
>>
>>Any suggestions on an approach would be greatly appreciated!
> 
> 
> 
> 
> Check the length of the list returned from split; this allows
> your to append to the previously extracted value if need be.
> 
> import StringIO
> import pprint
> 
> buf = """\
> myValue1 = contents of value1
> myValue2 = contents of value2 but
>                    with a new line here
> myValue3 = contents of value3
> """
> 
> mockfile = StringIO.StringIO(buf)
> 
> record=dict()
> 
> for line in mockfile:
>     kvpair = line.split('=', 2)

You want :
       kvpair = line.split('=', 1)

 >>> toto = "x = 42 = 33"
 >>> toto.split('=', 2)
['x ', ' 42 ', ' 33']


>     if len(kvpair) == 2:
>         key, value = kvpair
>         record[key] = value
>     else:
>         record[key] += line

Also, this won't handle the case where the first line doesn't contain an 
'='  (NameError, name 'key' is not defined)



More information about the Python-list mailing list