gpt4 book ai didi

python - 希望写入单独的文件; python :

转载 作者:太空宇宙 更新时间:2023-11-03 18:46:45 25 4
gpt4 key购买 nike

我希望将查询变量写入单独的文件。我已经研究过它,并且非常确定我需要将其移动到另一个文件并将其导入到该文件中。我非常愿意接受建议:

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()

最佳答案

à la Clippy:“看起来您正在尝试解析 URL”

来自the urlparse docs :

>>> 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()

关于python - 希望写入单独的文件; python :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19366953/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com