[英]How do I read multiple lines of data from a file into python?
I have a file on chemical compounds that gives the name of a section with the corresponding data, sometimes with a few lines of data, before it has a new section with a different name.我有一个关于化合物的文件,在它有一个具有不同名称的新部分之前,它给出了一个带有相应数据的部分的名称,有时还有几行数据。 I'm trying to read the 'NAME' entries (minus the 'NAME' part) and read each name (if it has multiple) into a list, then break whenever it reaches the 'FORMULA' section and have it move onto the next 'NAME' section, but I don't know how.
我正在尝试读取“NAME”条目(减去“NAME”部分)并将每个名称(如果它有多个)读入一个列表,然后在它到达“FORMULA”部分时中断并让它移动到下一个'NAME' 部分,但我不知道如何。 I'm a novice programmer.
我是一个新手程序员。 Here's an example: Compound List Screenshot
这是一个示例:化合物列表屏幕截图
Here's my code so far:到目前为止,这是我的代码:
li=[] #list of all names
for line in inputFile:
if line[:5]=='ENTRY':
items = line.split()
cmNm = items[1] #compound Number
else line[:4]=='NAME':
items = line.split()
cmName = items[]
if line[:7]=='FORMULA':
break
with open('/path/to/file.txt', 'r') as inputFile:
for line in inputFile:
try:
# Skip lines until we find an entry
while len(line) < 5 or line[:5] != 'ENTRY':
line = inputFile.next()
# Setup for logging that entry
cmNm = line.split()
cmName = []
# Append all name lines
while len(line) < 7 or line[:7] != 'FORMULA':
cmName.append(line)
line = inputFile.next()
# Process cmNm/cmName for current compound before moving on
print (str(cmNm) + " " + str(cmName))
except StopIteration:
pass # Reached end of file
cmNm contains the split list of the ENTRY line cmNm 包含 ENTRY 行的拆分列表
cmName contains a list of lines which together make up the name. cmName 包含一起构成名称的行列表。
You'll have to add whatever processing you want to store/format cmNm & cmName how you want it.您必须添加任何您想要存储/格式化 cmNm 和 cmName 的处理方式。 I just made it print them as it goes.
我只是让它打印出来。
You can safely pass
on StopIteration so long as the last valid entry has a FORMULA
.只要最后一个有效条目具有
FORMULA
您就可以安全地pass
StopIteration 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.