Python Split then Strip Array

Questions about programming languages and debugging
Post Reply
User avatar
FaoX
suck-o-fied!
suck-o-fied!
Posts: 76
Joined: 07 Sep 2007, 16:00
16
Contact:

Python Split then Strip Array

Post by FaoX »

So I have a database. I'm reading each line from it then I want to split each line by where the tab is and then strip the white space off both ends. Problem is I need to split the lines first since the only thing separating the data is tabs and spaces. Apparently you can not do a split before a strip?

Code: Select all

def read_txt_to_lines(fileName):
    with open(fileName, 'rU') as f:
        import csv
        fileLines = f.readlines()
        dataLines = []
        i=0
        for line in fileLines:
            l = line.strip().split('\t')
            i+=1
            dataLines.append(l)
        return dataLines
regions = read_txt_to_lines("simbad_new.tsv")
\"The OS is detected as NetBSD (it will even run on your toaster).\"

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Re: Python Split then Strip Array

Post by ayu »

Works fine for me

Code: Select all

>>> test = ' hello      world ';
>>> test.strip().split('\t');
['hello', 'world']
What result/error are you getting?
"The best place to hide a tree, is in a forest"

User avatar
FaoX
suck-o-fied!
suck-o-fied!
Posts: 76
Joined: 07 Sep 2007, 16:00
16
Contact:

Re: Python Split then Strip Array

Post by FaoX »

For some reason I'm getting left over white space. My first guess was that strip() was only removing spaces so I changed it to strip(' \t'). I was still left with white space usually after the string, but still occasionally before it. Looks like gibberish without context but this is the line after being split and stripped.

Code: Select all

['344', 'HD 81809               ', 'HD 81809                 ', 'SB*', '141.94491636 -06.07118951  ', '     ~', ' 6.02 ', ' 5.40 ', '     ~', '     ~', 'G1.5IV-V       ', ' 244', '   0']
\"The OS is detected as NetBSD (it will even run on your toaster).\"

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Re: Python Split then Strip Array

Post by ayu »

Try to print the values as hex and see what type of data is left at the end.
Do this without using strip (only do a split on the tab).

Code: Select all

>>> test = ' hello      world '
>>> test.strip().encode('hex')
'68656c6c6f09776f726c64'
"The best place to hide a tree, is in a forest"

User avatar
FaoX
suck-o-fied!
suck-o-fied!
Posts: 76
Joined: 07 Sep 2007, 16:00
16
Contact:

Re: Python Split then Strip Array

Post by FaoX »

Thanks for the help Cats but ended up fixing the problem at a different point in the program.
\"The OS is detected as NetBSD (it will even run on your toaster).\"

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Re: Python Split then Strip Array

Post by ayu »

FaoX wrote:Thanks for the help Cats but ended up fixing the problem at a different point in the program.
Awesome! : D
"The best place to hide a tree, is in a forest"

Post Reply