简体   繁体   English

如何使用Python跳过文件中的2行?

[英]How to skip 2 lines in a file with Python?

I have a series of files and I want to extract a specific number from each of them. 我有一系列文件,我想从每个文件中提取一个特定的数字。 In each of the files I have this line: 在每个文件中我都有这一行:

name, registration num

and exactly two lines after that there is the registration number. 并且正好有两行后面有注册号。 I would like to extract this number from each file. 我想从每个文件中提取这个数字。 and put it as a value of a dictionary.Anyone have any idea how it is possible ? 并把它作为一个字典的值。任何人都知道它是如何可能的?

my current code that does not actually work is like below: 我当前没有实际工作的代码如下所示:

matches=[]
for root, dirnames, filenames in os.walk('D:/Dataset2'):  
    for filename in fnmatch.filter(filenames, '*.txt'):   
        matches.append([root, filename])

filenames_list={}       
for root,filename in matches:
    filename_key = (os.path.join(filename).strip()).split('.',1)[0]

    fullfilename = os.path.join(root, filename)
    f= open(fullfilename, 'r')
    for line in f:
        if "<name, registration num'" in line:
            key=filename_key
            line+=2
            val=line

I usually use next() when I want to skip a single line, usually a header for a file. 当我想跳过一行时,我通常使用next() ,通常是文件的标题。

with open(file_path) as f:
    next(f) # skip 1 line
    next(f) # skip another one.
    for line in f:
        pass # now you can keep reading as if there was no first or second line.

Note: In Python 2.6 or earlier you must use f.next() 注意:在Python 2.6或更早版本中,您必须使用f.next()

One way would be to load the whole line into an array, and then read the line(s) you want from it. 一种方法是将整行加载到数组中,然后从中读取所需的行。 Example

A file called testfile contains the following: 名为testfile的文件包含以下内容:

A1
B2
C3
D4
E5

A program test.py: 一个程序test.py:

#!/usr/bin/env python

file = open('testfile')
lines = file.readlines()[2:]
file.close()

for line in lines:
    print(line.strip())

Output: 输出:

$./test.py
C3
D4
E5

EDIT: I read the question again, and noticed you just want a single line. 编辑:我再次阅读了这个问题,发现你只想要一行。 Then you could just remove the : , and use f.getlines()[2] to get the third line in a file 然后你可以删除: ,并使用f.getlines()[2]来获取文件中的第三行


  • Or you could use f.getline() three times, and just ignore the first two 或者你可以使用f.getline()三次,然后忽略前两个

  • Or you could use a for line in f type loop, and just ignore the first two line (have an incrementing counter) 或者您可以for line in f类型循环中使用for line in f ,并忽略前两行(具有递增计数器)

I suppose something like that would work... 我想这样的东西会起作用......

f= open(fullfilename, 'r')
for line in f:
    if "name, registration num" in line:
        key=filename_key
        break
f.readline()
res = f.readline()[:-1] #removed trailin newline
from itertools import islice
with open('data.txt') as f:
    for line in islice(f, 2, None):
        print line

Generally speaking, if you want to do something to a python iterator in-loop, like look two ahead, I find a good first place to look is to import itertools and look here . 一般来说,如果你想对循环中的python迭代器做一些事情,比如前面两个,我发现一个好的第一个看的地方是import itertools并看看这里 In your case, you might benefit from their implementation of consume . 在您的情况下,您可能会从他们的consume实施中受益。

Worth having a look to see if this issue hasn't been covered on SO before. 值得一看,看看之前是否还没有涵盖过这个问题。 Edit: Indeed- look here , which includes a good discussion of python iterators. 编辑:确实 - 看这里 ,其中包括对python迭代器的一个很好的讨论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM