i am having some problems with parsing lines. Actually i have everything in one line like this:

'test:[abc:123];something\r\nName=some name;phone no.: [123456]\r\nAddress: some address; another   address\r\n\r\n'

i would like to have this line like this:

test:[abc:123];something
Name=some name;phone no.: [123456]
Address: some address; another address

i have tried with

#from ast import literal_eval
from ast import *
a = 'test:[abc:123];something\r\nName=some name;phone no.: [123456]\r\nAddress: some address; another address\r\n\r\n'
msg = literal_eval(a)
# and
msg = eval(a)

but i get error. Is there some other easy way to work this out?

有帮助吗?

解决方案

kk = 'test:[abc:123];something\r\nName=some name;phone no.: [123456]\r\nAddress: some address; another address\r\n\r\n'
lk = "\n".join(kk.split("\r\n"))
print lk 

Output:

test:[abc:123];something
Name=some name;phone no.: [123456]
Address: some address; another   address

其他提示

If this a string you've generated then try .splitlines() of .split('\r\n')?

If this is read from a file then try setting the line endings when you open the file. Or open the file with myfile = open(filename, 'r', newline='\n\r')

Split works fine:

    l = 'test:[abc:123];something\r\nName=some name;phone no.: [123456]\r\nAddress: some address; another   address\r\n\r\n'.split("\r\n")
    In [14]: for line in l:
   ....:         print line
   ....:     
    test:[abc:123];something
    Name=some name;phone no.: [123456]
    Address: some address; another   address
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top