Python:如何解析下一行?

时间:2019-02-01 12:01:49

标签: python parsing

我对Python不那么有经验,因此我要求帮助我改善代码。

我正在尝试解析“名称”下面的“史蒂夫”:

xxxx xxxx xxxx Name
zzzz zzzz zzzz Steve

我的代码如下:

for line in myfile.readlines():
    [..]
    if re.search(r'Name =', line):
        print("Destination = ")
        samples+=line[15:19]
        nextline = "y"
    if nextline == 'y':
        samples+=line[15:19]

最终我将打印所有内容:

[..]    
for s in samples:
   myfile2.write(s)

它确实有效,但是我不相信没有更聪明的方法可以做到这一点(例如,一旦满足条件,就可以访问以下行。)

这是我需要解析的文件的示例。 但是结构可能会有所不同

#This is another example
Name =
Steve

感谢您的帮助。

3 个答案:

答案 0 :(得分:0)

list.txt:

zzzz zzzz zzzz Abcde
xxxx xxxx xxxx Name
zzzz zzzz zzzz Steve
zzzz zzzz zzzz Efghs

然后:

logFile = "list.txt"

with open(logFile) as f:
    content = f.readlines()    
# you may also want to remove empty lines
content = [l.strip() for l in content if l.strip()]

# flag for next line
nextLine = False

for line in content:
    find_Name = line.find('Name')       # check if Name exists in the line

    if find_Name > 0                    # If Name exists, set the next_line flag
        nextLine = not nextLine
    else:
        if nextLine:                    # If the flag is set, grab the Name
            print(line.split(" ")[-1])  # Grabbing the last word of the line
            nextLine = not nextLine

输出:

Steve

答案 1 :(得分:0)

不要重新发明轮子。使用csv模块,例如使用DictReader

import csv
with open("input") as f:
    reader = csv.DictReader(f, delimiter=" ")
    for line in reader:
        print(line["Name"])

这假设“史蒂夫”并不总是在字面上“名称”之下,因为如果其他列中的项目更长或更短,而同一列中的项目更长或更短,则位置可能会有所不同。此外,这还假定带有"Name"的行将是文件中的第一行。

如果不是这种情况,并且Name可以出现在任何行中,并且您只想在其下面的行中使用该名称,则可以只在{ next循环:

for

答案 2 :(得分:0)

list.txt:

zzzz zzzz zzzz Abcde
xxxx xxxx xxxx Name
zzzz zzzz zzzz Steve
zzzz zzzz zzzz Efghs

You can split each line on a space and then read the array index of interest.

如下所示:

logFile = "list.txt"

with open(logFile) as f:
    lines = f.readlines()

    for line in lines:
        # split using space
        result = line.split(" ")
        # you can access the name directly:
        #    name = line.split(" ")[3]
        # python array starts at 0
        # so by using [3], you access the 4th column.
        print result[3] 

或者,您可以使用numpy从数据字典中仅打印第4列:

import numpy
logFile = "list.txt"

data = []
with open(logFile) as f:
    lines = f.readlines()

    for line in lines:
        result = line.split(" ")
        data.append(result)

matrix = numpy.matrix(data)
print matrix[:,[3]]

您可以在此处了解更多信息: StackOverflow Question Some matrix info