Search code examples
pythonpython-3.xpython-requestspastebin

Unable to encode a unicode into a .txt file in python


So while trying to mess around with python i tried making a program which would get me the content from pastebin url's and then save each ones content into a file of their own. I got an error

This is the code :-

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

I get the following error :-

Traceback (most recent call last):
  File "C:\Users\Lenovo\Desktop\Py\Misc. Scripts\ai.py", line 9, in <module>
    x.write(r.text)
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python39\lib\encodings\cp1252.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>

Can somebody help?


Solution

  • You’re doing good at the start by reading in the input file as UTF-8. The only thing you’re missing is to do the same thing with your output file:

    x = open(f"{pastebin}.txt", "a+", encoding="utf-8")