所以当我试图用python弄乱的时候,我试着做一个程序,它会让我从pastebin url的内容,然后把每个内容保存到他们自己的文件中。我得到了一个错误
这是代码:-
import requests
file = open("file.txt", "r", encoding="utf-8").readlines()
for line in file:
link = line.rstrip("n")
n_link = link.replace("https://pastebin.com/", "https://pastebin.com/raw/")
pastebin = n_link.replace("https://pastebin.com/raw/", "")
r = requests.get(n_link, timeout=3)
x = open(f"{pastebin}.txt", "a+")
x.write(r.text)
x.close
我得到以下错误:-
Traceback (most recent call last):
File "C:UsersLenovoDesktopPyMisc. Scriptsai.py", line 9, in <module>
x.write(r.text)
File "C:UsersLenovoAppDataLocalProgramsPythonPython39libencodingscp1252.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character 'u2694' in position 9721: character maps to <undefined>
有人能帮忙吗?
您一开始就做得很好,以UTF-8格式读取输入文件。您唯一缺少的是对输出文件执行相同的操作:
x = open(f"{pastebin}.txt", "a+", encoding="utf-8")