0

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']
3
  • 4
    This question is off-topic because SO is not a code writing service Commented Jan 25, 2015 at 17:50
  • @BhargavRao, in fairness it is not overly obvious how to do it Commented Jan 25, 2015 at 17:58
  • @BhargavRao They did say they tried split(" ") and showed the input and expected output so I think it is a fair question, I don't think they expected the file part just how to split correctly which would not be that obvious to some people. Commented Jan 25, 2015 at 18:04

2 Answers 2

3

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]
Sign up to request clarification or add additional context in comments.

1 Comment

To guarantee that you get two elements after the split (I imagine it's plausible to not have flags), then line.rpartition(' ')[::2] can be used...
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.