About Python reading txt file overflow problem

Memory overflow when txt file is read

There are many ways to read TXT files. First of all, I will briefly introduce two for you:

method one

Read by line, add line by line to a two-dimensional array, so that you can perform an operation for each line, the code is as follows:

data = []
for line in open(txt_path):
	data.append(line)
print(data[])
	这种读取方式最后的结果是储存成一个二维数组,由于TXT中的数据都是字符串的形式读入,这样每行的处理可以放到for循环中,有利于操作。

Method Two

import pandas as pd
data = pd.read_scv(txt_path,sep = '')
print(data)

The advantage of this reading method is that the TXT file is read as an Excel, which corresponds to the operation of Excel, which is more convenient. And the first line in the TXT file can be used as an index.

Overflow problem

My TXT document has thousands of lines, and each line has a lot of content. In fact, I only need to read the data of the last line. When using method 1, the results of many runs show that no data is read.
Python may jump out and say: My storage is also limited!

problem solved

Since I just want to read the last line of the data, the solution to this problem is equivalent to how to read the last line of the document without taking up too much memory

method one

for line in open(txt_path):
	pass
print(line)

You might say that this is a wrong grammar, but this one really works

why?
Do you have a cow to tell me? I invite you to drink the first cup of milk tea in autumn

Method Two

line = []
for line in open(txt_path):
	pass
print(line

Since method one is not enough, it is always OK to add a line definition. This syntax should be OK when running under most compilers.

Method Three

data = []
line_num = 1
for line in open(txt_path):
	if line_num > 0:
		line_num = line_num + 1
		data.append(line)
	else:
		break
print(data[0])

This method can be used to read the last few lines, or read the TXT file from back to front, which is useful for pro-testing

Finally, after a long time of programming, I found that it is actually the most convenient to store data in Excel format. Excel is really easy to use. When calling, the data can be directly converted into the format when it was stored. It does not have to be changed into TXT files. String! !

Guess you like

Origin blog.csdn.net/qq_44647796/article/details/100867596