[英]Looping through lines of text file in python
我有兩個文本文件,我想逐行閱讀並檢查是否發生匹配,如果匹配,則打印或不執行任何操作。 但是在以下代碼中,它僅檢查第一個文件的第一行,並檢查第二個循環文件的所有行。 但是我想檢查第一個文件以及第二個文件的所有行。 我不確定自己在犯什么錯誤。
with open("changed_commands_from_default_value", "a") \
as changed_commands_from_default_value, \
open(command_file, "r") \
as command_executed_file, \
open("default_command_values", "r") \
as default_command_values:
for default_command in default_command_values:
for command_executed in command_executed_file:
only_command = command_executed.split()[0]
only_default_command = default_command.split()[0]
if only_command == only_default_command:
if command_executed != default_command:
print(" > The default value " +
default_command.rstrip() + " is changed to " +
command_executed.rstrip())
changed_commands_from_default_value.write(
"The default value " + '"' + default_command + '"' +
"is changed to " + '"' + command_executed + '"')
我的數據就像
File 1:
Data1 1
Data2 2
Data3 3
Data4 6
Data5 10
File 2:
Data1 4
Data2 4
Data3 6
....
我想有一個輸出
Data1 is changed from 1 to 4
Data2 is changed from 2 to 4 and so on...
要在兩個迭代器上“並行”循環,請使用內置的zip
,或者在Python 2中使用itertools.izip
(當然,后者需要在模塊開始時import itertools
)。
例如,更改:
for default_command in default_command_values:
for command_executed in command_executed_file:
變成:
for default_command, command_executed in zip(
default_command_values, command_executed_file):
假設這兩個文件確實是“平行的”-即以1-1對應的方式逐行顯示。 如果不是這種情況,那么最簡單的方法(除非文件太大,以至於您的內存無法容納它)是首先將一個讀入dict
,然后遍歷另一個使用dict
檢查。 因此,例如:
cmd2val = {}
with open("default_command_values", "r") as default_command_values:
for default_command in default_command_values:
cmd2val[default_command.split()[0]] = default_command.strip()
然后,分別:
with open(command_file, "r") as command_executed_file:
for command_executed in command_executed_file:
only_command = command_executed.split()[0]
if only_command not in cmd2val: continue # or whatever
command_executed = command_executed.strip()
if command_executed != cmd2val[only_command]:
# etc, etc, for all output you desire in this case
反之亦然,從預期較小的文件中構建字典,然后使用它逐行檢查預期較大的文件。
只需將讀取置於同一循環中即可。 兩個文件(分別名為t1.in和t2.in)的最小工作示例為:
with open('t1.in', 'r') as f1:
with open('t2.in', 'r') as f2:
while True:
l1, l2 = f1.readline(), f2.readline() # read lines simultaneously
# handle case where one of the lines is empty
# as file line count may differ
if (not l1) or (not l2): break
else:
# process lines here
本示例同時從兩個文件中讀取行,並且如果其中一個文件的行少於另一個min(lines_of_file_1, lines_of_file_2)
讀取min(lines_of_file_1, lines_of_file_2)
行。
#!/usr/bin/env python3
"""Match data in two files. Print the changes in the matched values.
Usage: %(prog)s <old-file> <new-file>
"""
import sys
if len(sys.argv) != 3:
sys.exit(__doc__ % dict(prog=sys.argv[0]))
old_filename, new_filename = sys.argv[1:]
# read old file
data = {}
with open(old_filename) as file:
for line in file:
try:
key, value = line.split()
data[key] = int(value)
except ValueError:
pass # ignore non-key-value lines
# compare with the new file
with open(new_filename) as file:
for line in file:
columns = line.split()
if len(columns) == 2 and columns[0] in data:
try:
new_value = int(columns[1])
except ValueError:
continue # ignore invalid lines
else: # matching line
value = data[columns[0]]
if value != new_value: # but values differ
print('{key} is changed from {value} to {new_value}'.format(
key=columns[0], value=value, new_value=new_value))
Data1 is changed from 1 to 4
Data2 is changed from 2 to 4
Data3 is changed from 3 to 6
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.