Python:使用另一个文件作为键从文件中提取行 [英] Python: Extracting lines from a file using another file as key
问题描述
我有一个看起来像这样的密钥"文件(MyKeyFile):
afdasdfa ghjdfghd wrtwertwt asdf(这些列中,但我没弄清楚格式,对不起)
我称这些键为键,它们与我要从源"文件中提取的行的第一个单词相同.因此,源文件(MySourceFile)看起来会像这样(再次,格式错误,但是第一列=键,随后的列=数据):
afdasdfa(多个制表符分隔的列) . . ghjdfghd(几个制表符分隔的列) . wrtwertwt . . asdf
和."会指示当前没有兴趣的行.
我是Python的绝对新手,这是我的发展方向:
with open('MyKeyFile','r') as infile, \
open('MyOutFile','w') as outfile:
for line in infile:
for runner in source:
# pick up the first word of the line in source
# if match, print the entire line to MyOutFile
# here I need help
outfile.close()
我意识到可能会有更好的方法来做到这一点.赞赏所有反馈-无论是通过我的解决方式还是更复杂的反馈方式.
谢谢 jd
据我了解(如果我输入错了,请在评论中告诉我),您有3个文件:
- MySourceFile
- MyKeyFile
- MyOutFile
您要:
- 从MyKeyFile读取密钥
- 从MySourceFile读取源代码
- 遍历源代码中的行
- 如果该行的第一个单词在键中:将该行附加到MyOutFile
- 关闭MyOutFile
代码如下:
with open('MySourceFile', 'r') as sourcefile:
source = sourcefile.read().splitlines()
with open('MyKeyFile', 'r') as keyfile:
keys = keyfile.read().split()
with open('MyOutFile', 'w') as outfile:
for line in source:
if line.split():
if line.split()[0] in keys:
outfile.write(line + "\n")
outfile.close()
I have a 'key' file that looks like this (MyKeyFile):
afdasdfa ghjdfghd wrtwertwt asdf (these are in a column, but I never figured out the formatting, sorry)
I call these keys and they are identical to the first word of the lines that I want to extract from a 'source' file. So the source file (MySourceFile) would look something like this (again, bad formatting, but 1st column = the key, following columns = data):
afdasdfa (several tab delimited columns) . . ghjdfghd ( several tab delimited columns) . wrtwertwt . . asdf
And the '.' would indicate lines of no interest currently.
I am an absolute novice in Python and this is how far I've come:
with open('MyKeyFile','r') as infile, \
open('MyOutFile','w') as outfile:
for line in infile:
for runner in source:
# pick up the first word of the line in source
# if match, print the entire line to MyOutFile
# here I need help
outfile.close()
I realize there may be better ways to do this. All feedback is appreciated - along my way of solving it, or along more sophisticated ones.
Thanks jd
As I understood (corrent me in the comments if I am wrong), you have 3 files:
- MySourceFile
- MyKeyFile
- MyOutFile
And you want to:
- Read keys from MyKeyFile
- Read source from MySourceFile
- Iterate over lines in the source
- If line's first word is in keys: append that line to MyOutFile
- Close MyOutFile
So here is the Code:
with open('MySourceFile', 'r') as sourcefile:
source = sourcefile.read().splitlines()
with open('MyKeyFile', 'r') as keyfile:
keys = keyfile.read().split()
with open('MyOutFile', 'w') as outfile:
for line in source:
if line.split():
if line.split()[0] in keys:
outfile.write(line + "\n")
outfile.close()
这篇关于Python:使用另一个文件作为键从文件中提取行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!