简体   繁体   English

如何将文件中的多行数据读入python?

[英]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.

相关问题 如何从多个URL读取Python中的HTML文件? - How do I read an HTML file in Python from multiple URLs? 如何使用 Python 从文件中读取数据? - How do I read data from a file using Python? 如何使用python一次从文件读取两行 - How do I read two lines from a file at a time using python 如何从python中的大数据文件中读取某些行? - How to read certain sets of lines from a big data file in python? 使用Python,如何从具有多个可变长度记录的二进制数据文件中读取和提取数据? - Using Python, how do I read and extract data from a binary data file with multiple variable-length records? Python Regex:如何使用正则表达式读取多行文件,并从每行中提取单词以创建两个不同的列表 - Python Regex: How do I use regular expression to read in a file with multiple lines, and extract words from each line to create two different lists Python:如何一次浏览文件中的多行? - Python: How do I go through multiple lines in a file at once? 如何在文件中搜索字符串并将其替换为Python中的多行? - How do I search a file for a string and replace it with multiple lines in Python? 如何合并我在 Python 中导入的文件中的行? - How do I merge lines from a file I import in Python? 如何在文件中搜索并从python中读取行 - how to search in a file and read lines from there in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM