0

I am trying to do a simple parsing on a text in python which I have no issues with in bash using tr '\n' ' '. Basically to get all of the lines on a single line. In python print line is a bit different from what I understand. re.sub cannot find my new line because it doesn't exist even though when I print to an output it does. Can someone explain how I can work around this issue in python?

Here is my code so far:

# -*- iso-8859-1 -*-
import re
def proc():
    f= open('out.txt', 'r')
    lines=f.readlines()
    for line in lines:
        line = line.strip()
        if '[' in line:
            line_1 = line
            line_1_split = line_1.split(' ')[0]
            line_2 = re.sub(r'\n',r' ', line_1_split)
            print line_2
proc()

Edit: I know that "print line," will print without the newline. The issue is that I need to handle these lines both before and after doing operations line by line. My code in shell uses sed, awk and tr to do this.

badner
  • 768
  • 2
  • 10
  • 31
  • `line.strip()` removes _all_ leading & trailing whitespace, including the `\n`. BTW, if you want to process line by line it's better to do `for line in f:` rather than reading the whole file into a list with `readlines` and then iterating over the list. – PM 2Ring Sep 19 '16 at 13:59
  • 2
    `print` adds a newline to the end of the output. See http://stackoverflow.com/q/493386/2800918 – CAB Sep 19 '16 at 14:08
  • @CAB make sense – badner Sep 19 '16 at 19:20

4 Answers4

1

You can write directly to stdout to avoid the automatic newline of print:

from sys import stdout
stdout.write("foo")
stdout.write("bar\n")

This will print foobar on a single line.

Emil H
  • 39,840
  • 10
  • 78
  • 97
1

When you call the print statement, you automatically add a new line. Just add a comma:

print line_2,

And it will all print on the same line.

Mind you, if you're trying to get all lines of a file, and print them on a single line, there are more efficient ways to do this:

with open('out.txt', 'r') as f:
    lines = f.readlines()
    for line in lines:
        line = line.strip()
        # Some extra line formatting stuff goes here
        print line, # Note the comma!

Alternatively, just join the lines on a string:

everything_on_one_line = ''.join(i.strip() for i in f.readlines())
print everything_on_one_line
Zizouz212
  • 4,908
  • 5
  • 42
  • 66
  • This works for the core problem but it isn't easy to handle operations on each line before and after. – badner Sep 19 '16 at 16:48
1

Using with ensures you close the file after iteration.

Iterating saves memory and doesn't load the entire file.

rstrip() removes the newline in the end.

Combined:

with open('out.txt', 'r') as f:
    for line in f:
        print line.rstrip(),
Bharel
  • 23,672
  • 5
  • 40
  • 80
  • This works but then you have to print to something and it isn't useful afterwards. It would be nice if I could var = print line, but it doesn't work taht way – badner Sep 19 '16 at 16:46
0

Use replace() method.

file = open('out.txt', 'r')
data = file.read()
file.close()
data.replace('\n', '')
Laszlowaty
  • 1,295
  • 2
  • 11
  • 19
  • This doesn't solve the issue. There weren't any lines in the string to begin with, so replacing won't do anything. The problem actually lies in the print statement. – Zizouz212 Sep 19 '16 at 14:04
  • In python3 it's possible to use `print('text', end='')` to escape new line. – Laszlowaty Sep 19 '16 at 14:08
  • This isn't python 3 though, it's python 2. The answer still misses the problem in the question. – Zizouz212 Sep 19 '16 at 14:10