从 Python 中的大型文本文件中读取数据块
Reading blocks of data from a large text file in Python
亲爱的,
我正在尝试读取一个非常大的文本文件 (>100 GB),其中包含不同变量之间的协方差。排列方式是第一个变量与所有变量相关,第二个变量与除第一个变量外的所有变量相关(例如图中的 14766203 或 line[0:19]),依此类推(参见图中的 1,2,3 ).这是我的示例数据:
14766203 -10.254364177 105.401485677 0.0049 0.0119 0.0024 0.0014 88.3946 7.340657124e-06 -7.137818870e-06 1.521836659e-06
3.367715952e-05 -6.261063214e-06
3.105358202e-06
14766204 6.126218197e-06 -7.264675283e-06 1.508365235e-06
-7.406839249e-06 3.152004956e-05 -6.020433814e-06
1.576663440e-06 -6.131501924e-06 2.813007315e-06
14766205 4.485532069e-06 -6.601931549e-06 1.508397490e-06
-7.243398379e-06 2.870296214e-05 -5.777139540e-06
1.798277242e-06 -6.343898734e-06 2.291452454e-06
14766204 -10.254727963 105.401101357 0.0065 0.0147 0.0031 0.0019 87.2542 1.293562659e-05 -1.188084039e-05 1.932569051e-06
5.177847716e-05 -7.850639841e-06
4.963314613e-06
14766205 6.259830057e-06 -8.072416685e-06 1.785233052e-06
-8.854538457e-06 3.629463550e-05 -6.703120240e-06
2.047196889e-06 -7.229432710e-06 2.917899913e-06
14766205 -10.254905775 105.400622259 0.0051 0.0149 0.0024 0.0016 88.4723 9.566876325e-06 -1.357014809e-05 2.378290143e-06
5.210766141e-05 -8.356178456e-06
4.016328161e-06
现在我希望能够将它们提取为 python 中的块,或者至少读取一个块并退出读取的文件(例如,1、2、3)。我没能成功,但这是我的努力:
with open(inFile, 'rb') as f: listData = []
for line in f:
MarkNumber = None;
if line[0:19].strip() != '' and line[23:36].strip() !='':
MarkNumber = str(line[0:19].strip())
if line[0:19].strip() == MarkNumber and len(line[23:36].strip()) !=0:
isMark = True
if line[0:19].strip() != MarkNumber and len(line[23:36].strip()) !=0:
isMark = False
if isMark == True:
ListOfData.append(line)
ListOfData 倾向于读取所有行,直到文件末尾。所以它并没有真正的帮助。
如能帮助解决此问题,我们将不胜感激。
谢谢 Nakhap
您能否使用正则表达式查找具有第二个数字块的块,例如,在一行开始的数字块的 15 个字符内?
import re
inFile = 'C:/path/myData.txt'
myregex = r'(^[.0-9e-]{1,15}[\W]{1,15}[.0-9e-])'
thisBlock = []
with open(inFile, 'rb') as f:
for line in f:
if re.search(re.compile(myregex),line):
print("Previous block finished. Here it is in a chunk:")
print(thisBlock)
print("\n\n\nNew block starting")
thisBlock = [line]
else:
thisBlock.append(line)
正则表达式 myregex
在两个数字序列 [.0-9e-]
之间查找 1 到 15 {1,15}
个空白字符 [\W]
-- 这会查找数字 0-9 ,以及小数点、负号和指数 e。表达式中的第一个 {1,15} 假定行开头的第一个数字表达式至少有 1 个字符,但少于 15 个字符。
亲爱的,
我正在尝试读取一个非常大的文本文件 (>100 GB),其中包含不同变量之间的协方差。排列方式是第一个变量与所有变量相关,第二个变量与除第一个变量外的所有变量相关(例如图中的 14766203 或 line[0:19]),依此类推(参见图中的 1,2,3 ).这是我的示例数据:
14766203 -10.254364177 105.401485677 0.0049 0.0119 0.0024 0.0014 88.3946 7.340657124e-06 -7.137818870e-06 1.521836659e-06
3.367715952e-05 -6.261063214e-06
3.105358202e-06
14766204 6.126218197e-06 -7.264675283e-06 1.508365235e-06
-7.406839249e-06 3.152004956e-05 -6.020433814e-06
1.576663440e-06 -6.131501924e-06 2.813007315e-06
14766205 4.485532069e-06 -6.601931549e-06 1.508397490e-06
-7.243398379e-06 2.870296214e-05 -5.777139540e-06
1.798277242e-06 -6.343898734e-06 2.291452454e-06
14766204 -10.254727963 105.401101357 0.0065 0.0147 0.0031 0.0019 87.2542 1.293562659e-05 -1.188084039e-05 1.932569051e-06
5.177847716e-05 -7.850639841e-06
4.963314613e-06
14766205 6.259830057e-06 -8.072416685e-06 1.785233052e-06
-8.854538457e-06 3.629463550e-05 -6.703120240e-06
2.047196889e-06 -7.229432710e-06 2.917899913e-06
14766205 -10.254905775 105.400622259 0.0051 0.0149 0.0024 0.0016 88.4723 9.566876325e-06 -1.357014809e-05 2.378290143e-06
5.210766141e-05 -8.356178456e-06
4.016328161e-06
现在我希望能够将它们提取为 python 中的块,或者至少读取一个块并退出读取的文件(例如,1、2、3)。我没能成功,但这是我的努力:
with open(inFile, 'rb') as f: listData = []
for line in f:
MarkNumber = None;
if line[0:19].strip() != '' and line[23:36].strip() !='':
MarkNumber = str(line[0:19].strip())
if line[0:19].strip() == MarkNumber and len(line[23:36].strip()) !=0:
isMark = True
if line[0:19].strip() != MarkNumber and len(line[23:36].strip()) !=0:
isMark = False
if isMark == True:
ListOfData.append(line)
ListOfData 倾向于读取所有行,直到文件末尾。所以它并没有真正的帮助。
如能帮助解决此问题,我们将不胜感激。
谢谢 Nakhap
您能否使用正则表达式查找具有第二个数字块的块,例如,在一行开始的数字块的 15 个字符内?
import re
inFile = 'C:/path/myData.txt'
myregex = r'(^[.0-9e-]{1,15}[\W]{1,15}[.0-9e-])'
thisBlock = []
with open(inFile, 'rb') as f:
for line in f:
if re.search(re.compile(myregex),line):
print("Previous block finished. Here it is in a chunk:")
print(thisBlock)
print("\n\n\nNew block starting")
thisBlock = [line]
else:
thisBlock.append(line)
正则表达式 myregex
在两个数字序列 [.0-9e-]
之间查找 1 到 15 {1,15}
个空白字符 [\W]
-- 这会查找数字 0-9 ,以及小数点、负号和指数 e。表达式中的第一个 {1,15} 假定行开头的第一个数字表达式至少有 1 个字符,但少于 15 个字符。