Home > Mobile >  How to separate floats from strings
How to separate floats from strings

Time:01-14

I have a tab delaminated txt file. One of the columns includes floats and then strings. I would like to NOT include everything after the strings. So instead of the lum list looking like .25 1,.47-4... my list would just have .25,.47 ...

enter image description here [enter image description here

Obviously right now my error reads "could not convert string to float: '.25 1"

any sugestions?

CodePudding user response:

You can easily take the value ".25 1" and use its .split() function for both ' ' and '-' then just use the first value in the list like so

val = '.25 1'
parts = val.split(' ' if ' ' in val else '-')
print(parts[0]) #Output is """ .25 """

or, if there are other characters in the string than a or -, you can create a new string while iterating over each character like this:

val = '.25 1'
newString = ''
for char in val:
    if not char.isdigit() and char != '.': break
    newString  = char
print(float(newString)) #Output is """ .25 """

CodePudding user response:

in future, please, use raw text (copy past) not screenshots.


lum = []
f = open('.\\test_big.txt')
lines = f.readlines()

for x in lines:
    data = x.split('\t')

    if ' ' in data[5]:
        part01 = data[5].split(' ')
        final_value = float(part01[0])

    if '-' in data[5]:
        # one line
        final_value = float(data[5].split('-')[0])

    lum.append(final_value)

print(lum)

Output: [0.25, 0.47, ...]

  •  Tags:  
  • Related