readlines

How to read a file without newlines?

How to read a file without newlines? Question: In Python, calling e.g. temp = open(filename,’r’).readlines() results in a list in which each element is a line from the file. However, these strings have a newline character at the end, which I don’t want. How can I get the data without the newlines? Asked By: Yotam …

Total answers: 12

How to create fake text file in Python

How to create fake text file in Python Question: How can I create a fake file object in Python that contains text? I’m trying to write unit tests for a method that takes in a file object and retrieves the text via readlines() then do some text manipulation. Please note I can’t create an actual …

Total answers: 4

Break string into list of characters in Python

Break string into list of characters in Python Question: Essentially I want to suck a line of text from a file, assign the characters to a list, and create a list of all the separate characters in a list — a list of lists. At the moment, I’ve tried this: fO = open(filename, ‘rU’) fL …

Total answers: 10

Does readlines() return a list or an iterator in Python 3?

Does readlines() return a list or an iterator in Python 3? Question: I’ve read in "Dive into Python 3" that: "The readlines() method now returns an iterator, so it is just as efficient as xreadlines() was in Python 2". See: Appendix A: Porting Code to Python 3 with 2to3: A.26 xreadlines() I/O method. I’m not …

Total answers: 3

Undo a file readline() operation so file-pointer is back in original state

Undo a file readline() operation so file-pointer is back in original state Question: I’m browsing through a Python file pointer of a text file in read-only mode using file.readline() looking for a special line. Once I find that line I want to pass the file pointer to a method that is expecting the file pointer …

Total answers: 5