Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Opening my input file in vi editor is like

ACT211111011100000000000000000000000606018^\10421040036991^\M/S DOHAR TRADERS^\BD^\167^\050^\000000579300^\000000579300^\
ACT211111011100000000000000000000000606018^\16711010000040^\M/S RAKIB TRADERS^\BD^\167^\050^\000100200386^\000100200386^\
ACT211111011100000000000000000000000606018^\16711010000101^\M-S, OVI ENTERPRISE^\BD^\167^\050^\000000000000^\000000000000^\
ACT211111011100000000000000000000000606018^\16711010000110^\MS. PUSPALATA CONSTRUCTION^\BD^\167^\050^\000000117900^\000000117900^\

I want to split the string like

ACT211111011100000000000000000000000606018 10421040036991 M/S DOHAR TRADERS BD 167 050 000000579300 000000579300

Here i have given the code that i am testing

#!/usr/bin/python
import os
flag220 = 0
f=file("/oasis/ist75/tmp/aa","r").readlines()
linecount=len(f)-1
for i in range(linecount):
        mycmds = []
        index = linecount-i-1
        line = f[index]
        split_line =  line.split()
        print split_line[0]
        if split_line[0].strip() == "ACT211111011100000000000000000000000606018":
            j=index-1
            flag220 = 0
            while j>-1:
                print f[j]
                flag220 = 1
                break
            j=j-1

        if flag220 == 1:
                for j in range (len(split_line)):
                    if split_line[j]=='050':
                        value = split_line[j+1]
                        print value
                        mycmd = './balance.sh ' + value + split_line[1].strip()
                        mycmds.append(mycmd)
                        break
for x in range(len(mycmds)):
    print mycmds[x]
    os.system(mycmds[x])
    mycmds=[]

The print split_line[0] output is looking like ACT21111101110000000000000000000000060601810421040036991M/S which is incorrect. please help me in this regards.

share|improve this question
Where does your code attempt to split on `^\`? – Ignacio Vazquez-Abrams May 30 at 10:43
1  
@IgnacioVazquez-Abrams I'm not sure it's a literal `^\` - I have a suspicion that's vim's way of displaying some control character, but could well be wrong... - but yup - don't see any attempt at splitting anywhere – Jon Clements May 30 at 10:45
2  
I'm sure it's 0x1c, but the code doesn't even try. – Ignacio Vazquez-Abrams May 30 at 10:50
You need to supply the separator to the split method. The only problem is that doing line.split('^\') results in a syntaxerror since the backslash makes the last quotation mark be included in the string, and doing line.split('^\\') also doesn't get me the correct result. To conclude: I'm also curious as to what the answer is.. – kramer65 May 30 at 11:01
1  
Use print repr(line) to find out the Python representation of the character. – Janne Karila May 30 at 11:38
show 3 more comments

2 Answers

It looks like you need to split on the ^\ characters as well. Regex works well for this:

import re
re.split('(\^\\|\s)', line)

That said, if that \^ isn't literal (which kind of looks to be the case) then you'll need to split on that instead.

share|improve this answer
thanks now the output of print split_line[0] is coming like ACT21111101110000000000000000000000060601810421040036991M/S DOHAR TRADERSBD167050000000579300000000579300. which should be ACT211111011100000000000000000000000606018 – tanvir May 30 at 11:06
i have also tried split_line = line.split('0x1c') that is same – tanvir May 30 at 11:10
1  
You need to find out what that character is that you're seeing in your editor. Alternatively if it's only occurring after a fixed length then you can build a regex on that. – ydaetskcoR May 30 at 11:29
up vote 0 down vote accepted

i have finally found the solution. It is like below

split_line = line.split('\x1c')

Thank you all for your kind co operation.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.