我希望将queryvars写出来分隔文件。我已经调查了它,我很确定我需要将它移动到另一个文件并将其导入该文件。我对建议很开放:
def nestForLoop():
lines = open("ampersand_right_split.txt", 'r').readlines()
f = open("newfile3.txt".format(), 'w')
for l in lines:
if "&" in l:
#param, value = str.split("?",1)
mainurl,_, query = l.partition('?')
queryvars = query.split("&")
if len(l) == 0:
break
print l
f.write(l)
f.close()
nestForLoop()
答案 0 :(得分:1)
àlaClippy:“看起来你正在尝试解析网址”
>>> from urlparse import urlparse
>>> o = urlparse('http://www.example.com/query%28%29.cgi?somevar=thing&someothervar=otherthing')
>>> o
ParseResult(scheme='http', netloc='www.example.com', path='/query%28%29.cgi', params='', query='somevar=thing&someothervar=otherthing', fragment='')
所以,要将它与你的例子结合起来:
from urlparse import urlparse
def nestForLoop():
lines = open("ampersand_right_split.txt", 'r').readlines()
with open("newfile3.txt".format(), 'w') as f:
for l in lines:
url = urlparse(l)
if url.query:
#param, value = str.split("?",1)
queryvars = url.query # Good to know, but why did we get this again?
if len(l) == 0:
break
print l
f.write(l)
nestForLoop()
答案 1 :(得分:0)
def nestForLoop():
lines = open("ampersand_right_split.txt", 'r').readlines()
f = open("newfile3.txt".format(), 'w')
g = open("newfile4.txt".format(), 'w')
for l in lines:
if "&" in l:
#param, value = str.split("?",1)
mainurl,_, query = l.partition('?')
queryvars = query.split("&")
if len(l) == 0:
break
print l
f.write(l)
g.write(queryvars)
f.close()
g.close()
有一种方法可以做到!