9

I have a huge input .txt file of this form:

0 1 0 1 0 0 0 0 0 0

0 1 0 1 0 0 0 0 0 0

0 1 0 1 0 0 0 0 0 0

and I want to delete all empty lines in order to create a new output .txt file like this:

0 1 0 1 0 0 0 0 0 0
0 1 0 1 0 0 0 0 0 0
0 1 0 1 0 0 0 0 0 0

I tried doing it with grep:

grep -v '^$' test1.txt > test2.txt 

but I get "SyntaxError: invalid syntax"

When I do it with pandas as someone suggests, I get different number of columns and some integers are converted into floats: e.g.: 1.0 instead of 1

When I do it as inspectorG4dget suggests (see below), it works nice, with only 1 problem: the last line is not printed completely:

with open('path/to/file') as infile, open('output.txt', 'w') as outfile:
    for line in infile:
        if not line.strip(): continue  # skip the empty line
        outfile.write(line)  # non-empty line. Write it to output

It must be something with my file then...

I've already addressed similar posts like these below (and others), but they are not working in my case, mainly due to the reasons explained above

How to delete all blank lines in the file with the help of python?

one liner for removing blank lines from a file in python?

Community
  • 1
  • 1
Lucas
  • 1,139
  • 3
  • 11
  • 23
  • 3
    What happens with those solutions? Are the newlines still there? Are you just printing the output, or actually rewriting the file (I think that first link just `print(line)`, rather than rewrite the file – dwanderson Jun 07 '16 at 15:04
  • 1
    Do you *have* to use Python for this? It is very easy to do this using something like `sed` or `grep`. – idjaw Jun 07 '16 at 15:07
  • Hi @dwanderson , I want t create a new file like the output. In some cases the newlines are still there, in others it prints it in a weird way like this: 0 1 0 1 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 – Lucas Jun 07 '16 at 15:08
  • Is there an equivalent of fgets in python? Because that will enter the line into a string and then you can use a length function to check if it is equal to 0. Then you can do whatever you want inside the conditional statement – Michael Jun 07 '16 at 15:10
  • @ idjaw , I know it is easy doing it with grep, but this would be part of a big python script which I want to automatise for doing calculations for many files, so it has to be Python – Lucas Jun 07 '16 at 15:11
  • @Lucas [this](http://stackoverflow.com/a/2369538/1832539) answer did not work for you at all? What happened exactly? I suggest you add these details to your question and not in the comments. Specify exactly what is happening from the solutions you tried. Show outputs as is, or even include any error messages you could be getting. – idjaw Jun 07 '16 at 15:15
  • @Lucas There is nothing stopping you from calling `grep/sed` from Python to make your life easier. – idjaw Jun 07 '16 at 15:17
  • Why is that file double-spaced? Was it generated on Windows (which uses `\r\n` line endings for text files) but you're reading it on Linux / Unix (which uses `\n` line endings)? If so, there's a very simple way to handle that. – PM 2Ring Jun 07 '16 at 15:38

2 Answers2

24

This is how I would do it:

with open('path/to/file') as infile, open('output.txt', 'w') as outfile:
    for line in infile:
        if not line.strip(): continue  # skip the empty line
        outfile.write(line)  # non-empty line. Write it to output
inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241
  • Thank you @inspectorG4dget. As I mentioned in my edited post, I tried your way, which works very nice, with the only problem that the last line is not printed completely. Before posting, I checked the similar post from which you marked "as duplicated" and I got the same problem, that´s why I thought the script had an error, but it could be my file... – Lucas Jun 07 '16 at 16:09
  • @Lucas: As you've mentioned in your post, I think that it's an issue with your file. Are you sure that it doesn't have any weird characters in it? Perhaps try printing it (or the `ord` values out) out to find out? – inspectorG4dget Jun 07 '16 at 17:44
0

You can use strip();

for line in yourTxtFile:
   if not line.strip():
      # write new file
      print (line)
erolkaya84
  • 1,769
  • 21
  • 28