I have a simple question about Python.
I wrote a working script but when I execute it, it gives me an answer in one line, as a string.
What I am looking is an answer to be on two separate lines.
Here is a code:
Python code
def test():
fh=open('xxxxxxx.txt', 'r')
fo=open('output.txt', 'a')
for line in fh:
line=line.strip()
if(line.startswith('Total Sequences')):
fo.write(line)
fh.close()
fh2=open('xxxxxxx.txt', 'r')
fo2=open('output.txt', 'a')
for line in fh2:
line=line.strip()
if(line.startswith('Sequence length')):
fo2.write(line)
fh2.close()
print(test())
You are removing the newline characters "\n" from each line in the file with the statement:-
line=line.strip()
Just remove it and it should work correctly.
Related
I have a text file.
Test.txt
this is line one; this line one
this is line two;
this is line three
I want to print line which contains semicolon but the semicolon should be at the end of line.
My code
search = open("Test.txt","r")
for line in search :
if ";" in line:
semi = line.split(";")
if semi[-1] == "\n":
print(line)
Output
this is line two;
My code is working fine but i want a better way to do this.
Can any one tell me short and most pythonic way to do this ?
For sure its easier
for line in search :
if line.endswith(';\n'):
print(line)
And as #IMCoins noted it's better to use context manager with to close your file when you're done working:
with open("Test.txt","r") as test_file:
for line in test_file:
if line.endswith(';\n'):
print(line)
Use the with keyword at first to open a file :
with open('foo.txt', 'r') as f:
for line in f:
if ';' in line:
semi = line.split(';')
if semi[-1] == '\n':
print line
For me, it is already mostly pythonic as you're using built-in function, with for loop.
if line[:-2] == ';\n':
print(line)
works correctly for python 2 & 3
also works if line is only a '/n'
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.
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.
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
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(),
Use replace() method.
file = open('out.txt', 'r')
data = file.read()
file.close()
data.replace('\n', '')
The input file: a.txt
aaaaaaaaaaaa
bbbbbbbbbbb
cccccccccccc
The python code:
with open("a.txt") as f:
for line in f:
print line
The problem:
aaaaaaaaaaaa
bbbbbbbbbbb
cccccccccccc
as you can see the output has extra line between each item.
How to prevent this?
print appends a newline, and the input lines already end with a newline.
A standard solution is to output the input lines verbatim:
import sys
with open("a.txt") as f:
for line in f:
sys.stdout.write(line)
PS: For Python 3 (or Python 2 with the print function), abarnert's print(…, end='') solution is the simplest one.
As the other answers explain, each line has a newline; when you print a bare string, it adds a line at the end. There are two ways around this; everything else is a variation on the same two ideas.
First, you can strip the newlines as you read them:
with open("a.txt") as f:
for line in f:
print line.rstrip()
This will strip any other trailing whitespace, like spaces or tabs, as well as the newline. Usually you don't care about this. If you do, you probably want to use universal newline mode, and strip off the newlines:
with open("a.txt", "rU") as f:
for line in f:
print line.rstrip('\n')
However, if you know the text file will be, say, a Windows-newline file, or a native-to-whichever-platform-I'm-running-on-right-now-newline file, you can strip the appropriate endings explicitly:
with open("a.txt") as f:
for line in f:
print line.rstrip('\r\n')
with open("a.txt") as f:
for line in f:
print line.rstrip(os.linesep)
The other way to do it is to leave the original newline, and just avoid printing an extra one. While you can do this by writing to sys.stdout with sys.stdout.write(line), you can also do it from print itself.
If you just add a comma to the end of the print statement, instead of printing a newline, it adds a "smart space". Exactly what that means is a bit tricky, but the idea is supposed to be that it adds a space when it should, and nothing when it shouldn't. Like most DWIM algorithms, it doesn't always get things right—but in this case, it does:
with open("a.txt") as f:
for line in f:
print line,
Of course we're now assuming that the file's newlines match your terminal's—if you try this with, say, classic Mac files on a Unix terminal, you'll end up with each line printing over the last one. Again, you can get around that by using universal newlines.
Anyway, you can avoid the DWIM magic of smart space by using the print function instead of the print statement. In Python 2.x, you get this by using a __future__ declaration:
from __future__ import print_function
with open("a.txt") as f:
for line in f:
print(line, end='')
Or you can use a third-party wrapper library like six, if you prefer.
What happens is that each line as a newline at the end, and print statement in python also adds a newline. You can strip the newlines:
with open("a.txt") as f:
for line in f:
print line.strip()
You could also try the splitlines() function, it strips automatically:
f = open('a.txt').read()
for l in f.splitlines():
print l
It is not adding a newline, but each scanned line from your file has a trailing one.
Try:
with open ("a.txt") as f:
for line in (x.rstrip ('\n') for x in f):
print line
I have a file named a.txt which looks like this:
I'm the first line
I'm the second line.
There may be more lines here.
I'm below an empty line.
I'm a line.
More lines here.
Now, I want to remove the contents above the empty line(including the empty line itself).
How could I do this in a Pythonic way?
Basically you can't delete stuff from the beginning of a file, so you will have to write to a new file.
I think the pythonic way looks like this:
# get a iterator over the lines in the file:
with open("input.txt", 'rt') as lines:
# while the line is not empty drop it
for line in lines:
if not line.strip():
break
# now lines is at the point after the first paragraph
# so write out everything from here
with open("output.txt", 'wt') as out:
out.writelines(lines)
Here are some simpler versions of this, without with for older Python versions:
lines = open("input.txt", 'rt')
for line in lines:
if not line.strip():
break
open("output.txt", 'wt').writelines(lines)
and a very straight forward version that simply splits the file at the empty line:
# first, read everything from the old file
text = open("input.txt", 'rt').read()
# split it at the first empty line ("\n\n")
first, rest = text.split('\n\n',1)
# make a new file and write the rest
open("output.txt", 'wt').write(rest)
Note that this can be pretty fragile, for example windows often uses \r\n as a single linebreak, so a empty line would be \r\n\r\n instead. But often you know the format of the file uses one kind of linebreaks only, so this could be fine.
Naive approach by iterating over the lines in the file one by one top to bottom:
#!/usr/bin/env python
with open("4692065.txt", 'r') as src, open("4692065.cut.txt", "w") as dest:
keep = False
for line in src:
if keep: dest.write(line)
if line.strip() == '': keep = True
The fileinput module (from the standard library) is convenient for this kind of thing. It sets things up so you can act as though your are editing the file "in-place":
import fileinput
import sys
fileobj=iter(fileinput.input(['a.txt'], inplace=True))
# iterate through the file until you find an empty line.
for line in fileobj:
if not line.strip():
break
# Iterators (like `fileobj`) pick up where they left off.
# Starting a new for-loop saves you one `if` statement and boolean variable.
for line in fileobj:
sys.stdout.write(line)
Any idea how big the file is going to be?
You could read the file into memory:
f = open('your_file', 'r')
lines = f.readlines()
which will read the file line by line and store those lines in a list (lines).
Then, close the file and reopen with 'w':
f.close()
f = open('your_file', 'w')
for line in lines:
if your_if_here:
f.write(line)
This will overwrite the current file. Then you can pick and choose which lines from the list you want to write back in. Probably not a very good idea if the file gets to large though, since the entire file has to reside in memory. But, it doesn't require that you create a second file to dump your output.
from itertools import dropwhile, islice
def content_after_emptyline(file_object):
return islice(dropwhile(lambda line: line.strip(), file_object), 1, None)
with open("filename") as f:
for line in content_after_emptyline(f):
print line,
You could do a little something like this:
with open('a.txt', 'r') as file:
lines = file.readlines()
blank_line = lines.index('\n')
lines = lines[blank_line+1:] #\n is the index of the blank line
with open('a.txt', 'w') as file:
file.write('\n'.join(lines))
and that makes the job much simpler.
I am a beginner and just started learning Python couple days ago (yay!)
so i've come across a problem. when i run, this code outputs everything but the text (txt in file is numbers 0-10 on seperate lines)
def output():
xf=open("data.txt", "r")
print xf
print("opened, printing now")
for line in xf:
print(xf.read())
print("and\n")
xf.close()
print("closed, done printing")
You don't use line, try:
with open('data.txt') as f:
for line in f:
print line
This should print out each number on its own line, like you want, in a lot less code, and more readable.
def output():
f = open('data.txt', 'r').read()
print f
When you used for line in xf: you basically already iterated over the file, implicitly reading each line.
All you need to do is print it:
for line in xf:
print(line)
The reason you aren't seeing the line output is because you aren't telling it to output the line. While iterating over values of line, you print xf.read(). The following is your function rewritten with this in mind. Also added is the use of a with statment block to automatically close the file when you're done with it.
(Using xf.close() is not wrong, just less pythonic for this example.)
def output():
with open("data.txt", "r") as xf:
print xf
print("opened, printing now")
for line in xf:
print(line)
print("and\n")
print("closed, done printing")
You have read the line of text into the variable line in the code for line in xf: so you need to show that e.g. print(line)
I would look at tutorials like the python.org one