ChatGPT解决这个技术问题 Extra ChatGPT

How to read a file without newlines?

In Python, calling

temp = open(filename,'r').readlines()

results in a list in which each element is a line in the file. It's a little stupid but still: readlines() also writes newline character to each element, something I do not wish to happen.

How can I avoid it?

Use strip: [l.strip('\n\r') for l in temp]. Or even rstrip. And since iteration here it can be in open instead of in temp.
I would be nice if in Python 3 there was a value to set open's newline argument to that chomped trailing newlines.

B
Boris Verkhovskiy

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.


Note that [line.rstrip('\n') for line in file] will remove more than one trailing \n.
More simply, [line[:-(line[-1] == '\n') or len(line)+1] for line in file] could instead be [line[:-(line[-1] == '\n') or None] for line in file].
These solutions read the entire file into memory. Changing the square brackets of a list comprehension to parentheses makes a generator expression which lets you iterate over the file one line at a time: for line in (x.strip() for x in f):
@velotron That's not really the point of the question/answer. Also: keep in mind that with closes the files when the block terminates, which means you cannot do with open(...) as f: lines = (line for line in f) and use lines outside the with because you'll get an I/O error. You can be lazy using a genexp, but you must consume it before closing the file.
@WesTurner. But there won't be more than one trailing newline. The extra newline will be part of the next empty line
A
Augustin
temp = open(filename,'r').read().splitlines()

What would happen with \r\n newlines though? ;)
Python automatically handles universal newlines, thus .split('\n') will split correctly, independently of the newline convention. It would matter if you read the file in binary mode.In that case splitlines() handles universal newlines while split('\n') doesn't.
And there's always os.linesep :)
@LarsH, it would help in some circumstances, on my system \r\n line endings are not converted to \n, whether read as text or binary, so os.linesep would work where \n does not. But splitlines is clearly the better choice, in the case you mention where the file does not match the os. Really I mostly mentioned it in case people looking at this discussion were unaware of its existence.
open() defaults to read mode. You don't have to pass 'r'.
m
mkrieger1

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]).


The read mode is the default mode. You don't have to pass the 'r' explicitly.
c
cieunteung

I think this is the best option.

temp = [line.strip() for line in file.readlines()]

This solution also removes leading and trailing spaces, which is not intended.
The comprehension is really nice, though. At least with Python 3, one can use temp = [line.rstrip() for line in file.readlines()] to get what @Roland_Illig notes is intended.
If you're going to iterate over all the lines, why do not so lazily? With .readlines(), you're effectively iterating over the entire file twice.
To be clear, the readlines() call is redundant, so this could be just temp = [line.strip() for line in file].
M
Marcel
temp = open(filename,'r').read().splitlines()

Are you sure this closes the file ? I think it does not, so it is not really a one-liner...
with is recommended to use for open-commands. For example: with open(file) as f: temp = f.read().splitlines()
S
SherylHohman

Try this:

u=open("url.txt","r")  
url=u.read().replace('\n','')  
print(url)  

While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, as this reduces the readability of both the code and the explanations!
I don't see why anyone should use this over some of the alternative solutions.
This only works, if the file contains exactly one line. If the file contains many lines, it removes the information, where each line ended.
Y
YScharf

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() != '']

updated to remove newline from each line.
m
marbel

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]

D
David Gilbertson

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


A
Abdelrahman Saleh

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


N
Necriss
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() 

Please add some explanation so that it will be useful to others.
You should use a context manager to handle the file object, and iterate over the file directly. By using .readlines() like this, you're effectively iterating over the entire file twice.
s
srus
import csv

with open(filename) as f:
    csvreader = csv.reader(f)
    for line in csvreader:
         print(line[0])

But what if the line has a comma in it?