简体   繁体   中英

readlines() not working in Jupyter Notebook

Novice level Python learner and working on macos Catalina with newly downloaded Python 3.8 installation, using Jupyter Notebook.

readlines() is returning all one line with \n, separators instead of individual lines.

foo = open('foo.txt')
foo.seek(0)
foo.readlines()

Returns:

['Something on line 1.\n', 'Something on line 2.\n', 'Something on line 3.']

The foo.txt file looks like this:

Something on line 1.
Something on line 2.
Something on line 3.

Am I right in thinking this is not expected behaviour? I can't find a similar query here. Thank you.

Edit - this is how it appears in Jupyter Notebook on the tutorial. Perhaps that is a Notebook setting?

图片

Am I right in thinking this is not expected behaviour?

No, that's what's supposed to happen in any python editor.

readlines() is returning all one line with \n, separators instead of individual lines.

If you want multiple lines, just do this:

foo = open('foo.txt')
foo.seek(0)
foo.read()

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