Python 从文件中读取行而不获取\";附在末尾

Python 从文件中读取行而不获取\";附在末尾,python,linux,file-io,ubuntu-10.04,Python,Linux,File Io,Ubuntu 10.04,我的文件是“xml.txt”,包含以下内容: books.xml news.xml mix.xml 如果我使用readline()函数,它会在所有文件的名称后面附加“\n”,这是一个错误,因为我想打开xml.txt中包含的文件。我写道: fo = open("xml.tx","r") for i in range(count.__len__()): #here count is one of may arrays that i'm using file = fo.readline()

我的文件是“xml.txt”,包含以下内容:

books.xml 
news.xml
mix.xml
如果我使用readline()函数,它会在所有文件的名称后面附加“\n”,这是一个错误,因为我想打开xml.txt中包含的文件。我写道:

fo = open("xml.tx","r")
for i in range(count.__len__()): #here count is one of may arrays that i'm using
    file = fo.readline()
    find_root(file) # here find_root is my own created function not displayed here
运行此代码时遇到错误:

IOError: [Errno 2] No such file or directory: 'books.xml\n'

您可以使用字符串对象的
.rstrip()
方法来获取删除了尾随空格(包括换行符)的版本

例如:


要仅删除末尾的换行符,请执行以下操作:

line = line.rstrip('\n')

readline
保留换行符的原因是,您可以区分空行(有换行符)和文件结尾(空字符串)。

更好的方式是为文件使用上下文管理器,
len()
而不是调用
\len\uu()


要删除结尾处的换行符,还可以使用如下内容:

for line in file:
   print line[:-1]


我计时只是出于好奇。下面是一个大文件的结果

with open(FILENAME, "r") as file:
    lines = file.read().split("\n")
tldr; 文件读取然后拆分似乎是处理大文件的最快方法

with open(FILENAME, "r") as file:
    lines = file.read().split("\n")
但是,如果您仍然需要在这些行中循环,那么您可能需要:

with open(FILENAME, "r") as file:
    for line in file:
        line = line.rstrip("\n")
Python 3.4.2

import timeit


FILENAME = "mylargefile.csv"
DELIMITER = "\n"


def splitlines_read():
    """Read the file then split the lines from the splitlines builtin method.

    Returns:
        lines (list): List of file lines.
    """
    with open(FILENAME, "r") as file:
        lines = file.read().splitlines()
    return lines
# end splitlines_read

def split_read():
    """Read the file then split the lines.

    This method will return empty strings for blank lines (Same as the other methods).
    This method may also have an extra additional element as an empty string (compared to
    splitlines_read).

    Returns:
        lines (list): List of file lines.
    """
    with open(FILENAME, "r") as file:
        lines = file.read().split(DELIMITER)
    return lines
# end split_read

def strip_read():
    """Loop through the file and create a new list of lines and removes any "\n" by rstrip

    Returns:
        lines (list): List of file lines.
    """
    with open(FILENAME, "r") as file:
        lines = [line.rstrip(DELIMITER) for line in file]
    return lines
# end strip_readline

def strip_readlines():
    """Loop through the file's read lines and create a new list of lines and removes any "\n" by
    rstrip. ... will probably be slower than the strip_read, but might as well test everything.

    Returns:
        lines (list): List of file lines.
    """
    with open(FILENAME, "r") as file:
        lines = [line.rstrip(DELIMITER) for line in file.readlines()]
    return lines
# end strip_readline

def compare_times():
    run = 100
    splitlines_t = timeit.timeit(splitlines_read, number=run)
    print("Splitlines Read:", splitlines_t)

    split_t = timeit.timeit(split_read, number=run)
    print("Split Read:", split_t)

    strip_t = timeit.timeit(strip_read, number=run)
    print("Strip Read:", strip_t)

    striplines_t = timeit.timeit(strip_readlines, number=run)
    print("Strip Readlines:", striplines_t)
# end compare_times

def compare_values():
    """Compare the values of the file.

    Note: split_read fails, because has an extra empty string in the list of lines. That's the only
    reason why it fails.
    """
    splr = splitlines_read()
    sprl = split_read()
    strr = strip_read()
    strl = strip_readlines()

    print("splitlines_read")
    print(repr(splr[:10]))

    print("split_read", splr == sprl)
    print(repr(sprl[:10]))

    print("strip_read", splr == strr)
    print(repr(strr[:10]))

    print("strip_readline", splr == strl)
    print(repr(strl[:10]))
# end compare_values

if __name__ == "__main__":
    compare_values()
    compare_times()
结果:

run = 1000
Splitlines Read: 201.02846901328783
Split Read: 137.51448011841822
Strip Read: 156.18040391519133
Strip Readline: 172.12281272950372

run = 100
Splitlines Read: 19.956802833188124
Split Read: 13.657361738959867
Strip Read: 15.731161020969516
Strip Readlines: 17.434831199281092

run = 100
Splitlines Read: 20.01516321280158
Split Read: 13.786344555543899
Strip Read: 16.02410587620824
Strip Readlines: 17.09326775703279
文件读取然后拆分似乎是处理大文件的最快方法

with open(FILENAME, "r") as file:
    lines = file.read().split("\n")
注意:读取然后拆分(“\n”)将在列表末尾有一个额外的空字符串

注意:read-then splitlines()检查的内容不仅仅是“\n”可能是“\r\n”

“如果”条件将检查该行是否有字符串,如果是,下一行将删除结尾处的“\n”并写入文件。
代码已测试。;)

带有@Lars Wirzenius答案的用例:

with open("list.txt", "r") as myfile:
    for lines in myfile:
        lines = lines.rstrip('\n')    # the trick
        try:
            with open(lines) as myFile:
                print "ok"
        except IOError as e:
            print "files does not exist"

你能告诉我语法吗?我的意思是如何以及在哪里添加这个?这个解决方案将删除所有尾随空格,而不仅仅是换行符。如果读取的行是
'foo\n'
,则
.rstrip()
将返回
'foo'
,而
'foo'
是问题陈述所要求的。不要使用
计数。
而不是
计数!尽管该问题特别询问了
“\n”
字符,但还有一个更普遍的问题,即读取一行时不带行尾,无论该文件的行尾是什么。几乎所有的答案都没有提到这一点。(Daniel F.似乎是这样)你忘了提到好的Python风格还包括不隐藏带有你自己名字的内置程序,比如
文件
…@martineau,是的,我不推荐它,就让它溜走了
# mode : 'r', 'w', 'a'
f = open("ur_filename", "mode")
for t in f:
    if(t):
        fn.write(t.rstrip("\n"))
with open("list.txt", "r") as myfile:
    for lines in myfile:
        lines = lines.rstrip('\n')    # the trick
        try:
            with open(lines) as myFile:
                print "ok"
        except IOError as e:
            print "files does not exist"