[英]Python 3 - Reading line by line from a textfile and doing action for each line
I need something like this done: 我需要做这样的事情:
So far here's what I've done. 到目前为止,这是我所做的。
if loginAccount(user, pass, url) == 1:
if someActionChecking(url) == 1:
None
else:
print ("[INFO]blablabla")
actionNow(url, actionProcess)
if someActionChecking(url2) == 1:
None
else:
print ("[INFO]blablabla")
actionNow(url2, actionProcess)
if someActionChecking(url3) == 1:
None
else:
print ("[INFO]blablabla")
actionNow(url3, actionProcess)
else:
print ("Login failed.")
Now here's the problem : 现在这是问题所在 :
I want to shorten the code and make my life easier by just fetching the url's from a text file that contains url1, url2, url3 (as an example), read the URL's stored there line by line, and loop the actionNow() function per line. 我想缩短代码并简化工作,只需从包含url1,url2,url3的文本文件中获取url(例如), 逐行读取存储在该URL的URL,然后逐个循环actionNow()函数线。 So it will be something like this:
因此它将是这样的:
if loginAccount(user, pass, url) == 1:
if someActionChecking(url) == 1:
None
else:
#I need to loop this process below!!
print ("[INFO]blablabla")
actionNow(url, actionProcess)
else:
print ("Login failed.")
Any workaround for this? 任何解决方法? I am already searching for a couple of days now, and all I see was the strip() technique but I can't really understand how it works.
我现在已经搜索了几天,而我所看到的只是strip()技术,但我真的不明白它是如何工作的。
Hope someone can guide me, 希望有人可以指导我,
urlFile = "path/to/file"
urls = open(urlFile).readLines()
for url in urls:
doAction(url)
OR you could just put it all in the url file reading loop; 或者,您可以将其全部放入url文件读取循环中;
with open(urlFile) as f:
for line in f:
doAction(line)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.