Search code examples
pythontexttext-processing

How to keep blank lines while reading text file Python


Reading in a generic text file with blank lines, i.e. no tabs, no spaces along some lines. I want to use these blank lines as a delimiter to process the files in the program, but I cannot stop them from being stripped while reading in the file.

I have been trying to use genfromtxt: data = np.genfromtxt(path, encoding = "utf-8-sig", dtype = str, delimiter = "none", autostrip = False) with no success: I cannot figure out how to maybe replace the blank lines with something else, or to keep them as an empty string (notice from the line of code I am trying to read in each line of the text file as one string).

Maybe regex or pandas is more useful? I have tried but cannot make use of them.

To finally re-iterate problem: I want to read in a text file with completely blank lines, and keep said blank lines in the resulting array of strings, without adding anything to the text files pre-reading in.

Let me know if any more information would be useful, or point me to a question I've potentially duplicated. Thanks.


Solution

  • with open(path, 'r', encoding='utf-8-sig') as f:
        data = f.readlines()