0
votes

I have a text file as following:

enter image description here

[’23.5', '1.9', '11.3', '25.5', '2.7', '22.5', '3.4', '3.4', '2.9', '2.0', '2.0', '2.1', '36.0', '13.5', '7.8', '5.9', '3.2', '2.0', '13.0', '2.0', '2.4', '11.3', '28.5', '22.7', '2.6', '2.2', '7.0', '12.5', '18.2', '7.2', '13.5', '14.0', '2.4', '2.3', '2.1', '2.0', '5.3', '3.2', '3.1', '2.6', '20.0', '13.0', '1.9', '2.2', '2.3', '22.7', '30.5', '45.5', '2.8', '1.9', '30.0', '2.2', '2.0', '3.2', '2.0', '2.2', '2.2', '4.8', '2.8', '17.5', '2.1', '2.5', '3.6', '2.4', '2.0', '1.9', '2.3', '2.1', '2.0', '2.6', '3.0', '14.0', '2.0', '2.2', '12.5', '38.5', '38.0', '13.0', '3.2', '3.7', '3.7', '2.0', '2.1', '2.1', '9.3', '8.0', '27.0', '13.5', '9.3', '15.5', '17.5', '3.5', '2.0', '27.0', '2.0', '1.9', '23.2', '15.5', '6.5', '2.1', '23.0', '15.0', '3.8', '19.0', '29.0', '101.5', '26.5', '16.5', '14.0', '13.5', '10.3', '2.8', '16.0', '2.6', '24.5', '30.5', '13.0', '2.1', '2.3', '2.2', '15.0', '34.0', '2.1', '2.9', '6.8', '2.7', '12.3', '7.1', '28.5', '30.0', '8.7', '13.0', '6.0', '9.0', '15.0', '15.5', '8.0', '15.0', '1.9', '36.0', '17.0', '17.5', '28.0', '4.2', '9.0', '5.3', '7.8', '6.2', '15.5', '2.2', '14.0', '30.0', '12.5', '2.6', '10.0', '4.0', '6.7', '17.5', '2.7', '31.5', '3.8', '2.7', '21.5', '6.1', '23.0', '19.8', '2.8', '2.8', '2.2', '8.8', '1.9', '2.1', '20.5', '3.6', '10.7', '2.0', '18.7', '12.7', '2.1', '1.9', '3.0', '14.5', '2.6', '2.0', '20.7', '3.3', '29.0', '19.5', '10.0', '2.8', '4.0', '4.2', '2.4', '32.0', '2.2', '29.0', '2.4', '2.0', '20.0', '3.2', '2.2', '4.2', '2.8', '17.2', '2.0', '7.5', '15.7', '4.5', '2.8', '5.7', '2.2', '1.8', '2.4', '2.0', '2.2', '2.1', '2.0', '2.2', '2.0', '2.8', '8.0', '2.5', '3.3', '2.1', '2.0', '2.0', '2.0', '2.1', '19.5', '13.8', '9.2', '5.5', '2.5', '2.2', '2.1', '3.1', '8.6', '2.3', '3.7', '10.8', '2.1', '2.0', '2.4', '20.0', '', '', '', '', '', '', '7', '', '', '2.0', '17.4', '8.7', '3.9', '2.0', '2.0', '5.9', '3.1', '22.8', '4.1', '2.8', '7.6', '4.9', '2.7', '26.5', '2.1', '2.2', '2.4', '2.0', '3.8', '5.7', '2.8', '14.0', '2.2', '2.0', '27.5', '32.5', '3.1', '30.0', '20.5', '19.5', '25.0', '9.0', '3.7', '26.0', '13.5', '15.0', '1.9', '2.2', '3.7', '2.0', '2.2', '2.3', '2.1', '2.1', '1.9', '2.0', '2.0', '2.4', '2.3', '2.5', '16.0', '6.5', '19.5', '2.0', '4.1', '9.4', '51.5', '2.1', '15.4', '2.0', '2.1', '2.8', '2.1', '2.1', '2.6', '2.9', '20.0', '2.5', '13.0', '2.2', '1.9', '2.2', '2.3', '2.2', '2.1', '3.4', '19.5', '22.8', '2.6', '2.2', '1.9', '2.4', '2.0', '6.6', '3.2', '1.9', '2.0', '2.6', '2.0', '14.0', '2.0', '3.0', '19.8', '5.4', '3.6', '5.9', '2.2', '3.6', '2.0', '2.0', '9.8', '2.4', '3.3', '3.0', '28.0', '47.0', '5.8', '3.1', '20.5', '18.0', '32.0', '4.4', '2.1', '3.2', '2.6', '3.2', '2.5', '3.5', '1.9', '2.2', '2.3', '2.5', '2.8', '2.0', '2.4', '5.0', '6.3', '5.1', '2.5', '2.0', '2.9', '2.0', '2.4', '2.2', '3.3', '5.0', '2.6', '2.7', '1.9', '3.0', '2.1', '2.4', '2.7', '16.5', '5.2', '2.2', '2.2', '3.2', '5.5', '24.5', '17.5', '2.1', '2.7', '3.6', '2.2', '2.1', '8.1', '3.1', '2.1', '2.9', '2.1', '2.1', '2.0', '2.3', '1.9', '13.7', '3.2', '2.1', '2.6', '5.1', '23.5', '24.5', '3.1', '2.6', '3.2', '2.2', '23.0', '2.5', '8.2', '2.0', '3.7', '2.0', '2.1', '2.4', '2.7', '3.1', '2.6', '2.7', '3.2', '3.4', '4.6', '2.9', '3.3', '10.8', '2.5', '3.5', '2.0', '1.9', '2.2', '2.1', '2.1', '2.1', '2.1', '2.1', '31.8', '14.4', '1.9', '2.8', '5.0', '2.5', '2.5', '3.0', '4.3', '2.4', '2.4', '4.8', '2.0', '1.9', '2.1', '3.8', '2.5', '', '', '', '', '', '', '7', '', '', '2.0', '3.4', '2.2', '3.5', '2.1', '2.4', '2.5', '3.0', '2.1', '2.0', '5.6', '2.3', '4.9', '1.9', '1.9', '21.5', '2.4', '1.9', '2.0', '2.3', '2.8', '23.3', '6.4', '2.0', '1.9', '2.2', '2.7', '6.3', '2.5', '2.7', '2.1', '14.0', '18.7', '2.0', '2.9', '67.0', '2.2', '2.2', '2.3', '2.1', '3.1', '2.5', '2.4', '2.5', '2.1', '2.0', '28.0', '7.2', '3.1', '37.5', '17.5', '2.0', '30.0', '2.4', '2.2', '2.2', '29.0', '4.9', '2.7', '2.2', '2.2', '3.5', '4.2', '1.9', '2.0', '3.8', '1.9', '2.4', '2.8', '19.5', '16.0', '18.5', '2.9', '20.5', '3.1', '1.9', '2.3', '2.3', '30.0', '13.0', '3.2', '2.2', '2.6', '2.1', '47.0', '2.6', '2.5', '1.9', '23.3', '4.9', '3.6', '2.5', '2.1', '2.1', '17.0', '1.9', '2.7', '2.3', '2.6', '1.9', '16.5', '2.7', '3.3', '2.4', '2.2', '3.3', '2.2', '2.8', '2.1', '3.4', '3.4', '1.9', '2.0', '3.7', '2.0', '21.5', '2.1', '2.2', '2.3', '2.3', '6.8', '1.9', '2.8', '3.3', '5.4', '125.0', '2.7', '39.3', '24.0', '30.5', '2.4', '2.5', '10.0', '2.6', '2.3', '2.3', '2.4', '3.5', '2.4', '2.1', '2.0', '2.7', '18.5', '2.4', '2.0', '6.1', '4.8', '2.4', '2.1', '2.5', '2.2', '2.6', '2.0', '2.2', '2.1', '2.3', '2.1', '2.4', '2.1', '2.2', '2.9', '12.7', '12.5', '2.0', '3.0', '2.1', '2.6', '3.2', '3.0', '2.0', '2.5', '3.8', '4.9', '3.2', '1.9', '10.2', '2.0', '1.9', '9.0', '3.5', '6.6', '3.1', '2.2', '4.9', '6.7', '1.9', '5.5', '3.5', '2.2', '2.0', '2.2', '7.3', '2.1', '2.6', '5.3', '2.9', '1.9', '2.2', '2.6', '2.3', '1.9', '3.0', '2.3', '2.4', '1.9', '2.2', '2.2', '3.2', '28.8', '19.0', '2.5', '7.6', '11.4', '4.6', '2.3', '16.5', '37.5', '1.9', '2.3', '27.0', '18.5', '2.1', '31.0', '2.4', '29.5', '31.0', '2.1', '2.2', '2.4', '1.9', '2.2', '29.3', '11.1', '4.1', '29.0', '18.0', '21.0', '10.5', '2.5', '', '']

I want to remove empty string (ex: in the red column '') and use the scripts

f= open('filename.txt','r') 
A1=f.read()

for i in A1:
    if i=="":
        A1.remove(i)
    print A1
    break

however the empty strings of file were still not removed

I am very confused, please help me @@

7
Is the "empty string" at the end of the file spaces? Newlines? Empty ' ' sections?Celeo
open("output.txt","w").write(str(filter(None,literal_eval(open("input.txt").read())))Joran Beasley
why would you think its a good idea to post text as an image? ...Joran Beasley
can you post it? I don't understand you comment @@?GregMaddux
why not paste the content of the filename.txt as text?Dyno Fu

7 Answers

1
votes

assuming your input.txt file really looks like that, you can json.load it to a list, filter out all falsy values(empty strings), and finally json.dump it back to an output file

import json
with open("output.txt","wb") as f:
     cleaned_data = filter(None,json.load(open("input.txt")))
     json.dump(f,cleaned_data)
1
votes

First, ensure that A1 is actually a list:

isinstance(A1, list)

If so, just use this list comprehension to remove the empty quotes.

A1 = [i for i in A1 if i != '' and i != ', ']

If A1 is actually a string, first convert it to a list:

from ast import literal_eval 

A1 = literal_eval(A1)

Also, you cannot safely modify a list while iterating over it, which is why I used list comprehension to generate a new filtered list from the original one.

Finally, if you want the values as floats instead of strings:

A1 = [float(i) if isinstance(float(i), float) else i 
      for i in A1 if i != '' and i != ', ']
0
votes

and it won't let you remove a element while iterate over it.

l = [line for line in A if line.strip()]
print l
0
votes

I'm personally a fan of higher-order functions, filter makes it super easy to do what you want:

A1 = filter (lambda x: x != '', open("fname.txt", 'r').read().split(' '))

Filter takes in two arguments; the first is an anonymous function that only exists in the execution of the filter function. The filter function expects the anonymous function to return a boolean value. The anonymous function takes in one parameter, the current element of the array, and determines whether it fits a certain criterion. In this case, if the element is not equal to a null string, then it's included in new list. The other argument to filter is an iterable list, which I built by reading in the file and splitting it at spaces.

You can do some neat things with the higher-order functions built into most languages that would otherwise need iteration or other boring, normal things to do :)

Here's a guide to Python's higher-order functions.

0
votes

Looks like what you have is a dump of a Python list to a text file. Let's get it back out of that text file, shall we?

from ast import literal_eval    

with open('path/to/file.txt') as f:
    result = ''.join(f)
    ## note that it's unclear from your question whether the text file
    ## is actually multiple lines, or just word wrapped. It it's just
    ## word wrapped, `result = f.read()` is sufficient

result_lst = literal_eval(result)  # this includes the empty strings
result_lst = filter(None, literal_eval(result))  # this doesn't
0
votes

As there is no built-in function for float's in python, I would first make a simple function for that, import the text file and then let list comprehension do the rest:

def isnumber(s):
    try:
        float(s)
        return True
    except ValueError:
        pass
    try:
        import unicodedata
        unicodedata.numeric(s)
        return True
    except (TypeError, ValueError):
        pass
    return False

with open(filename, 'r') as f:
    l = f.read()

result = [a for a in list(l.split("'")) if is_number(a)]
-1
votes

try to change the line

if i=="":

to

if i == "''" or i == '""'