我正在使用Python 3处理结果文件。文件的结构是字符串标识符的组合,后跟此格式的整数值列表:
ENERGY_BOUNDS
1.964033E+07 1.733253E+07 1.491825E+07 1.384031E+07 1.161834E+07 1.000000E+07 8.187308E+06 6.703200E+06
6.065307E+06 5.488116E+06 4.493290E+06 3.678794E+06 3.011942E+06 2.465970E+06 2.231302E+06 2.018965E+06
EIGENVALUE
1.219034E+00
该文件中可能有50套具有唯一标识符的不同数据集。我想做的是编写一个代码,该代码将搜索特定的标识符(例如ENERGY_BOUNDS),然后将其后的值读入列表,并在下一个标识符(在本例中为EIGENVALUE)停止。然后,我需要能够操作该列表(找到其长度,打印其值等)。
我将其编写为一个函数,因此当我要搜索不同的标识符时,可以在代码中多次调用它。到目前为止,我所拥有的是:
def read_data_from_file(file_name, identifier):
list_of_results = [] # Create list_of_results to put results in for future manipulation
# Open the file in read only mode
with open(file_name, 'r') as read_obj:
# Read all lines in the file one by one
for line in read_obj:
# For each line, check if line contains the string
if identifier in line:
# If yes, read the next line
nextValue = next(line)
list_of_results.append(nextValue.rstrip())
return list_of_results
它可以正常工作,直到读取标识符之后的下一行为止,而我将继续研究如何继续读取该行之后的结果以及如何使其停在下一个标识符处。
以下是简单且经过测试的答案。
你犯了两个错误
line
是一个字符串而不是迭代器,这样做next(line)
会导致错误。以下是对代码进行少量修改后的代码。还对您的数据进行了测试
def read_data_from_file(file_name, identifier):
with open(file_name, 'r') as read_obj:
list_of_results = []
# Read all lines in the file one by one
for line in read_obj:
# For each line, check if line contains the string
if identifier in line:
# If yes, read the next line
nextValue = next(read_obj)
while(not nextValue.strip().isalpha()): #keep on reading untill next identifier appears
list_of_results.extend(nextValue.split())
nextValue = next(read_obj)
print(list_of_results)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句