我有2个文件:"a.txt"
和"b.txt"
我希望匹配它们之间的行。这些文件包含以下内容:
1
2
3
4
5
6
7
8
9
10
为了匹配这些行,我正在做以下
a = open("a.txt","r")
b = open("b.txt","r")
for al in a:
al = al.split()
val_a = al[0]
for bl in b:
bl = bl.split()
val_b = bl[0]
print val_a, val_b
令人惊讶的是,print语句ONLY
打印出以下内容:
1 1
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
似乎只有一次访问a上的循环。我尝试调试的内容如下:
for al in a:
al = al.split()
val_a = al[0]
print val_a
for bl in b:
bl = bl.split()
val_b = bl[0]
此处的print语句打印a
有人可以帮我解释一下吗?
答案 0 :(得分:4)
每次尝试循环时,都需要将文件指针重置为b.txt
文件的开头,否则你已经到了最后。
最简单的方法是使用file.seek(0)
,如下所示:
a = open("a.txt","r")
b = open("b.txt","r")
for al in a:
al = al.split()
val_a = al[0]
b.seek(0)
for bl in b:
bl = bl.split()
val_b = bl[0]
print val_a, val_b
答案 1 :(得分:3)
您可以使用b
将readlines()
提取到一个行列表,然后您可以反复迭代它:
a = open("a.txt","r")
b = open("b.txt","r").readlines()
for al in a:
al = al.split()
val_a = al[0]
for bl in b:
bl = bl.split()
val_b = bl[0]
print val_a, val_b
答案 2 :(得分:1)
试试这个:
import mechanicalsoup
def fetchfile(query):
url = "http://www.italgiure.giustizia.it/sncass/"
browser = mechanicalsoup.Browser()
page = browser.get(url)
search_form = page.soup.find("form", {"id": "z-form"})
search_form.find("input", {"id":"searchterm"})["value"] = query
response = browser.submit(search_form, page.url)
print(response) # the response is 200, so it should be a good sign
# actual parsing will come later...
print("1235" in response.text) # quick-check to see if there is what I'm looking for, but I get False
# in fact this...
print(page.text == response.text) # ...gives me True
fetchfile("1235/2012")
说明:
a = open("a.txt","r")
b = open("b.txt","r")
for i,j in zip(a,b):
print (i.split()[0])
print (j.split()[0])
答案 3 :(得分:0)
将b转换为列表,否则第一次迭代将通过b将使用该文件。
blist= list(b)
然后是内循环
For bl in blist:
...