Search code examples
pythonunicodebeautifulsoup

UnicodeEncodeError when writing to a file


I am trying to write some strings to a file (the strings have been given to me by the HTML parser BeautifulSoup).

I can use "print" to display them, but when I use file.write() I get the following error:

UnicodeEncodeError: 'ascii' codec can't encode character u'\xa3' in position 6: ordinal not in range(128)

How can I parse this?


Solution

  • I tried this it works fine

    with open(r"C:\rag\sampleoutput.txt", 'w', encoding="utf-8") as f: