Use*_*YmY 3 python dictionary count line
我有一系列文件,我想从每个文件中提取一个特定的数字.在每个文件中我都有这一行:
name, registration num
Run Code Online (Sandbox Code Playgroud)
并且正好有两行后面有注册号.我想从每个文件中提取这个数字.并把它作为一个字典的值.任何人都知道它是如何可能的?
我当前没有实际工作的代码如下所示:
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
Run Code Online (Sandbox Code Playgroud)
我通常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.
Run Code Online (Sandbox Code Playgroud)
注意:在Python 2.6或更早版本中,您必须使用 f.next()