简体   繁体   中英

python, split string into array after second character

I'm capturing a file with the following content:

*> <'Char><'Char><'space><'string>*

example: Original File

Fs .file1
 M /home/file2
?? /home/file3
M  /home/file4
 D /home/file5

I'm trying to get each line into a list and get always one array with 2 columns. I tried

line.split(' ') 

but it is not working since an empty char might happen on the first or second position of each row.

so, I need to do this split after the second character, meaning the result of the above file will be:

['Fs','.file1']
[' M','./home/file2']
['??','./home/file3']
['M ','./home/file4']
[' D','./home/file5']

It would be also acceptable if the empty character on the firl array index is trimmed

['Fs','.file1']
['M','./home/file2']
['??','./home/file3']
['M ','./home/file4']
['D','./home/file5']

Use rsplit presuming the contents all look like you have in your question:

lines ="""Fs .file1
 M /home/file2
?? /home/file3
M  /home/file4
 D /home/file5"""

for line in lines.splitlines():
    # splitting once on " " starting from the right 
    # side keep all whitespace on the left elements
    print(line.rsplit(" ",1))

['Fs', '.file1']
[' M', '/home/file2']
['??', '/home/file3']
['M ', '/home/file4']
[' D', '/home/file5']

So simply use the following in your own code:

print [line.rstrip().rsplit(" ",1)for line in f]

Or as @jonClements suggests use line.rpartition(' ')[::2] to always make sure we get two elements per list:

print [line.rpartition(' ')[::2] for line in f]

If there are always 3 characters before the filename (fixed width) then do the simple way:

flags, filename = line[:2], line[3:]

There is no need to do fancy instead of the right thing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM