简体   繁体   中英

Why does readlines() include a \n character in my text file if it doesn't exist?

I am attempting to read a file and collect every character which includes the following: "\\n" "\\t" " "

My text file contains the following:

aaabbcddd

I intentionally left out \\n, so python should stop reading at d.

The code is as follows:

fname  = "files.txt"
content = []
with open(fname) as f:
    for each_line in f:
        for each_character in each_line:
            content.append(each_character)


print(content)

My output for content is:

['a', 'a', 'a', 'b', 'b', 'c', 'd', 'd', 'd', '\n']

It is fine if '\\n' is stored in my list if it was intentionally inserted; however, in this instance, I did not put '\\n'.

What went wrong?

You read a line.

A line is terminated by a newline character. I am pretty sure that your text editor inserts that at the end of what you typed.

Try adding a simple trace of what you get on input:

for each_line in f:
    print(len(each_line), "|", each_line, "|")

My output:

10 | aaabbcddd
 |
['a', 'a', 'a', 'b', 'b', 'c', 'd', 'd', 'd', '\n']

Even though I did not add a return character to the file, and it has only a single line, the editor (vi) assumes it. The input string has 9 letters and the newline.

Are you sure your text file does not have a newline after this string? I've tried running your code as is, and it does not include the '\\n' in the array. This is what is outputted:

['a', 'a', 'a', 'b', 'b', 'c', 'd', 'd', 'd']

Sometimes a newline could be inserted accidentally in the text file and it doesn't hurt to double check.

也许文本编辑器会自动保存换行符?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM