2012-07-01 27 views
12

我的文件是「xml.txt」具有以下內容附加:讀取從Python文件行沒有得到「 n」末

books.xml 
news.xml 
mix.xml 

,如果我使用的ReadLine()函數,它追加「\ n「,因爲我想打開xml.txt中包含的文件,因此所有文件的名稱都是錯誤的。我寫了這個:

IO錯誤:[錯誤2]沒有這樣的文件或目錄: 'Books.xml的\ n'

+3

不要使用'計數.__ LEN __()''但LEN(計數)'! –

+0

雖然這個問題特別提出了關於'\ n'字符的問題,但是還有一個更爲普遍的問題,那就是在沒有換行的情況下閱讀一行代碼,不管代碼是否爲文件。幾乎所有的答案都沒有解決這個問題。 (丹尼爾F.的似乎)。 – brianmearns

回答

32

要在最後只刪除換行符:

line = line.rstrip('\n') 

原因readline保持換行符,所以你可以在一個空行區分(有行)和文件的結尾(空字符串)。

+0

tHanks,它的工作:) –

+0

簡單而直接的重點。優秀的解決方 – Jiraheta

6

您可以使用.rstrip()上運行該代碼中遇到

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 

錯誤字符串對象的方法來獲取帶有尾隨空白(包括換行符)的版本。

如:

find_root(file.rstrip()) 
+0

你能告訴我語法嗎?我的意思是我應該如何和在哪裏添加這個? –

+0

請參閱編輯示例。 – Amber

+0

謝謝:)它的工作 –

1

這是更好的風格使用上下文管理器的文件,並len()而不是調用.__len__()

with open("xml.tx","r") as fo: 
    for i in range(len(count)): #here count is one of may arrays that i'm using 
     file = next(fo).rstrip("\n") 
     find_root(file) # here find_root is my own created function not displayed here 
+1

你忘了提及,良好的Python風格還包括不隱藏自己的名字,比如'文件'的內置... – martineau

+0

@martineau,是的,我讓那一個幻燈片,因爲它已被棄用 –

1

要刪除換行符來回結束時,你也可以使用這樣的事情:

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

我只是爲了好奇才計時。以下是各種大文件的結果。

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 

文件讀取然後拆分似乎是緊固件一個大文件上的st方法。

注:讀取然後拆分(「\ n」)將在列表的末尾有一個額外的空字符串。

注意:閱讀然後splitlines()檢查更多,然後只是「\ n」可能「\ r \ n」。

0
# mode : 'r', 'w', 'a' 
f = open("ur_filename", "mode") 
for t in f: 
    if(t): 
     fn.write(t.rstrip("\n")) 

「如果」條件將檢查線路是否有串與否,如果是的下一行,將去掉「\ n」末和寫入文件。 代碼測試。 ;)

1

用例與@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"