gpt4 book ai didi

python - 从 Python 3 中的值中删除 b'

转载 作者:行者123 更新时间:2023-12-01 09:33:59 24 4
gpt4 key购买 nike

尝试从输出中删除 b'

word_site = "http://link.de/prox.txt"
response = requests.get(word_site)
WORDS = response.content.splitlines()
print (random.choice(WORDS))

Prints b'116.11.254.37:80'

当我尝试

word_site = "http://link.de/prox.txt"
response = requests.get(word_site)
WORDS = response.content.splitlines()
print (WORDS.decode('utf-8'))


print (random.choice(WORDS))

输出是:

    print (WORDS.decode('utf-8'))
AttributeError: 'list' object has no attribute 'decode'

我做错了什么? .txt 链接只有几行代理。

最佳答案

splitlines返回一个列表,你必须对列表的每个元素进行操作

word_site = "http://link.de/prox.txt"
response = requests.get(word_site)
WORDS = response.content.splitlines()
utf8_words = [w.decode('utf-8') for w in WORDS]
print (utf8_words)


print (random.choice(utf8_words))

更好(来自megalng):

word_site = "http://link.de/prox.txt"
response = requests.get(word_site)
WORDS = response.content.decode('utf-8').splitlines()
print (WORDS)


print (random.choice(WORDS))

关于python - 从 Python 3 中的值中删除 b',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49700680/

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