Python - delete empty line from .txt, but last line don't delete - python

i have a test.txt file like this:
1 - test
2 -
3 - test
4 -
(the numbers are just for example)
and my python code:
with open('test.txt') as infile, open('output.txt', 'w') as outfile:
for line in infile:
if not line.strip(): continue # skip the empty line
outfile.write(line)
but the output.txt is:
1 - teste
2 - teste
3 -
I'd like to delete the last line too, but NOT with the code that erase the last line like this:
lines = file.readlines()
lines = lines[:-1]
How can i delete this last line checking with python if is a empty line?
Thanks!

The reason why you have an empty line at the end is because the last line ends with a newline. To remove the newline from the last line, you can read all the lines into a list, and then strip the last one:
with open('test.txt') as infile, open('output.txt', 'w') as outfile:
# remove empty lines
lines = [line for line in infile if line.strip()]
# remove the newline from the last line
if lines:
lines[-1] = lines[-1].rstrip('\r\n')
# write everything to disk
outfile.writelines(lines)

Here a solution:
with open('test.txt') as infile, open('output.txt', 'w') as outfile:
lines = "\n".join([line.strip() for line in infile if line.strip()])
outfile.writelines(lines)

Related

Printing the first paragraph from infile to outfile

I have one file containing a speech, and have an empty output file. I am trying to print the first paragraph of the speech (read infile) and print it out to the outfile using if/else statement.
the program isn't bugging but its not outputting to my outfile.
file = open("/Users/newuser/Desktop/MLKspeech.txt", "r")
file2 = open("/Users/newuser/Desktop/mlkparagraph.txt", "w")
content = file.read()
for j in content:
if (j == ""):
continue
elif (j == "\n"):
file2.write(content)
else:
break
Assuming paragraphs are separated by an empty line, you can iterate on the file line-by-line and write them to the new file, until an empty line is reached. An empty line can be discovered with str.isspace():
with open("MLKspeech.txt") as in_file, open("mlkparagraph.txt", 'w') as out_file:
for line in in_file:
if line.isspace():
break
out_file.write(line)
Assuming your paragraphs are separated with the '\t' character, you could try this:
with open('file1.txt', mode='rt') as file:
breakpoint = file.read().find('\t')
file.seek[0]
with open('file2.txt', mode='wt') as file2:
file2.write(file.read()[:breakpoint])
The goal is to capture the first few lines of your input file until you read an empty line (where there is either nothing or only a newline character). One way of doing that is to iterate through each line in the text with f.readlines() and store only the lines that you need in a list, breaking when you read an empty line:
content = []
with open('infile.txt') as f:
for line in f.readlines():
if line in ('', '\n'):
break
content.append(line)
You can then write each line to your output file:
with open('outfile.txt', 'w') as f:
for line in content:
f.write(line)

Trouble with sys.stdin.readline [duplicate]

In Python, calling e.g. temp = open(filename,'r').readlines() results in a list in which each element is a line from the file. However, these strings have a newline character at the end, which I don't want.
How can I get the data without the newlines?
You can read the whole file and split lines using str.splitlines:
temp = file.read().splitlines()
Or you can strip the newline by hand:
temp = [line[:-1] for line in file]
Note: this last solution only works if the file ends with a newline, otherwise the last line will lose a character.
This assumption is true in most cases (especially for files created by text editors, which often do add an ending newline anyway).
If you want to avoid this you can add a newline at the end of file:
with open(the_file, 'r+') as f:
f.seek(-1, 2) # go at the end of the file
if f.read(1) != '\n':
# add missing newline if not already present
f.write('\n')
f.flush()
f.seek(0)
lines = [line[:-1] for line in f]
Or a simpler alternative is to strip the newline instead:
[line.rstrip('\n') for line in file]
Or even, although pretty unreadable:
[line[:-(line[-1] == '\n') or len(line)+1] for line in file]
Which exploits the fact that the return value of or isn't a boolean, but the object that was evaluated true or false.
The readlines method is actually equivalent to:
def readlines(self):
lines = []
for line in iter(self.readline, ''):
lines.append(line)
return lines
# or equivalently
def readlines(self):
lines = []
while True:
line = self.readline()
if not line:
break
lines.append(line)
return lines
Since readline() keeps the newline also readlines() keeps it.
Note: for symmetry to readlines() the writelines() method does not add ending newlines, so f2.writelines(f.readlines()) produces an exact copy of f in f2.
temp = open(filename,'r').read().split('\n')
Reading file one row at the time. Removing unwanted chars from end of the string with str.rstrip(chars).
with open(filename, 'r') as fileobj:
for row in fileobj:
print(row.rstrip('\n'))
See also str.strip([chars]) and str.lstrip([chars]).
I think this is the best option.
temp = [line.strip() for line in file.readlines()]
temp = open(filename,'r').read().splitlines()
My preferred one-liner -- if you don't count from pathlib import Path :)
lines = Path(filename).read_text().splitlines()
This it auto-closes the file, no need for with open()...
Added in Python 3.5.
https://docs.python.org/3/library/pathlib.html#pathlib.Path.read_text
Try this:
u=open("url.txt","r")
url=u.read().replace('\n','')
print(url)
To get rid of trailing end-of-line (/n) characters and of empty list values (''), try:
f = open(path_sample, "r")
lines = [line.rstrip('\n') for line in f.readlines() if line.strip() != '']
You can read the file as a list easily using a list comprehension
with open("foo.txt", 'r') as f:
lst = [row.rstrip('\n') for row in f]
my_file = open("first_file.txt", "r")
for line in my_file.readlines():
if line[-1:] == "\n":
print(line[:-1])
else:
print(line)
my_file.close()
This script here will take lines from file and save every line without newline with ,0 at the end in file2.
file = open("temp.txt", "+r")
file2 = open("res.txt", "+w")
for line in file:
file2.writelines(f"{line.splitlines()[0]},0\n")
file2.close()
if you looked at line, this value is data\n, so we put splitlines()
to make it as an array and [0] to choose the only word data
import csv
with open(filename) as f:
csvreader = csv.reader(f)
for line in csvreader:
print(line[0])

write to previous line in python

I am writing code to generate words from one file to another
I have done all ok but the problem that when I use line it feed a new line in the output file I want to next word after line to be written at the same line
the code
with open("test.txt") as f:
with open("out.txt", "w") as f1:
for line in f:
f1.write("<answer>" + line +"doit");
now doit comes in a new line in the out.txt
the text file has 3 lines
door
window
house
The problem is that your variable line contains a \n at the end, which you have to remove yourself:
with open("test.txt") as f:
with open("out.txt", "w") as f1:
for line in f:
f1.write("<answer>" + line[:-1] +"doit")
The problem of using rstrip as the other answer suggests is that you would lose ending spaces: ' aa \n'.rstrip() gives you ' aa'. This might or might not be what you need.
Use rstrip() to remove the trailing \n
with open("test.txt") as f:
with open("out.txt", "w") as f1:
for line in f:
f1.write("<answer>" + line.rstrip('\n') +"doit");

How to ignore if line already exists in writing file in python?

I have file1.txt which contains lines as
list 0
list 1
line 1
In file2.txt i want to write only if the line is not already exists in file2.txt
my code:
fo=open("file1.txt","r")
fin=open("file2.txt","a")
lines=fo.readlines()
for line in lines:
if "list" in line:
fin.write(line)
for line in lines:
if "li" in line:
fin.write(line)
Output: It is printing the lines twice.Here I want to write only once if the same line is repeated.
list 0
list 1
list 0
list 1
line 1
My output should be
list 0
list 1
line 1
My suggestion would be, to first read all lines of file2.txt and put them into a suitable datastructure (i.e. a Set).
Then reopen file2.txt in append mode, iterate over all lines of file1.txt and write only these that are not in the set (here, the in operator comes handy...)
with open("file2.txt", "r") as f2:
lineset = set(f2)
with open("file2.txt", "a") as f2:
with open("file1.txt", "r") as f1:
for line in f1:
if not line in lineset:
d2.write(line)
This will read all the lines in file2 and only write a line to file2 if its not already there. It will also close your file automatically by using the excellent "with" statement in python. :)
with open("file1.txt","r") as file1, open("file2.txt", "w+") as file2:
lines2 = file2.readlines()
for line in file1:
if line not in lines2:
file2.write(line)
If you want to use list iteration, the same code is just 2 lines, but I prefer the readability of the first version.
with open("file1.txt", "r") as file1, open("file2.txt", "w+") as file2:
[file2.write(line) for line in file1 if line not in file2.readlines()]
Use a set to track the collection of lines in the file2.txt file.
fo=open("file1.txt","r")
fin=open("file2.txt","a")
lines=fo.readlines()
# Rewing the file so that we can read it's contents.
fin.seek(0)
existing_lines = set(fin)
for line in lines:
if line not in existing_lines:
fin.write(line)
existing_lines.add(line)
You would want to do something like:
fo=open("file1.txt","r")
fin=open("file2.txt","a")
linesOut=fo.readlines()
linesIn=fin.readlines()
for lineOut in linesOut:
#check each line in linesIn to see if it contains lineOut
writeLine=True
for lineIn in linesIn:
if lineOut==lineIn:
writeLine=False
#if not add it
if writeLine:
fin.write(lineOut)

make list from text file and compare the lists

The full.txt contains:
www.example.com/a.jpg
www.example.com/b.jpg
www.example.com/k.jpg
www.example.com/n.jpg
www.example.com/x.jpg
The partial.txt contains:
a.jpg
k.jpg
Why the following code does not provide the desired result?
with open ('full.txt', 'r') as infile:
lines_full=[line for line in infile]
with open ('partial.txt', 'r') as infile:
lines_partial=[line for line in infile]
with open ('remaining.txt', 'w') as outfile:
for element in lines_full:
if element[16:21] not in lines_partial: #element[16:21] means like a.jpg
outfile.write (element)
The desired remaining.txt should have those elements of full.txt that are not in partial.txt exactly as follows:
www.example.com/b.jpg
www.example.com/n.jpg
www.example.com/x.jpg
you can use os.path library:
from os import path
with open ('full.txt', 'r') as f:
lines_full = f.read().splitlines()
with open ('partial.txt', 'r') as f:
lines_partial = set(f.read().splitlines()) # create set for faster checking
lines_new = [x + '\n' for x in lines_full if path.split(x)[1] not in lines_partial]
with open('remaining.txt', 'w') as f:
f.writelines(lines_new)
This code will include the newline character at the end of each line, which means it will never match "a.jpg" or "k.jpg" precisely.
with open ('partial.txt', 'r') as infile:
lines_partial=[line for line in infile]
Change it to
with open ('partial.txt', 'r') as infile:
lines_partial=[line[:-1] for line in infile]
to get rid of the newline characters (line[:-1] means "without the last character of the line")

Categories

Resources