gpt4 book ai didi

python - 当字符串列表交换为文本文件时程序停止工作

转载 作者:太空宇宙 更新时间:2023-11-04 04:22:27 24 4
gpt4 key购买 nike

我的程序过滤用户名列表并检查它们在 Twitter 上的可用性。在测试中,我使用了一个我手动输入代码的列表。但是,一旦它开始工作,我就将它们换成了一个 txt 文件。只是注意到实际上它不再成功检查文本文件中的前 4 个名称,而是检查最后一个名称。

我已经在失败的 4 次测试中打印了 json 输出,它是“只使用字母、数字和‘_’”

available = open("Available-Twitter.txt", "a")

with open ("accounts.txt", "r") as usernames:
for username in usernames:
r = requests.get("https://twitter.com/users/username_available?username={}".format(username))
print (r.json()['msg'])
print (' ')

if "!" in (r.json()['msg']):
print(Fore.GREEN + "Account available - {}".format(username))
available.write(username+"\n")
else:
print(Fore.RED + "Account is unavailable - {}".format(username))

最佳答案

文件生成器生成以换行符结尾的行。如果你不需要它,你应该剥离它:

with open ("accounts.txt", "r") as usernames:
for username in usernames:
username = username.rstrip()
...

关于python - 当字符串列表交换为文本文件时程序停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54224250/

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