Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UnicodeEncodeError: 'charmap' codec can't encode character u'\u2026' in position 139: character maps to <undefined> #1

Open
baditaflorin opened this issue Nov 23, 2016 · 20 comments

Comments

@baditaflorin
Copy link

C:\LEARN PYTHON\sirajology\twitter_sentiment_challenge>python demo.py
Traceback (most recent call last):
File "demo.py", line 27, in
print(tweet.text)
File "C:\Python27\lib\encodings\cp852.py", line 12, in encode
return codecs.charmap_encode(input,errors,encoding_map)
UnicodeEncodeError: 'charmap' codec can't encode character u'\u2026' in position 139: character maps to

@prisar
Copy link

prisar commented May 3, 2017

run chcp 65001 at first.

steps:

  1. chcp 65001

  2. python demo.py

@ravindukaluarachchi
Copy link

encode the text when printing

print(tweet.text.encode("utf-8"))

@hygull
Copy link

hygull commented Mar 14, 2018

@ravindukaluarachchi suggested good.

It works fine.

public_tweets = api.home_timeline()
for tweet in public_tweets:
    # print(tweet.text);
    print ((tweet.text).encode('utf8')) 

@kevenex
Copy link

kevenex commented Dec 17, 2018

UnicodeEncodeError: 'UCS-2' codec can't encode characters in position 44-44: Non-BMP character not supported in Tk

Getting this error

@hygull
Copy link

hygull commented Dec 19, 2018

UnicodeEncodeError: 'UCS-2' codec can't encode characters in position 44-44: Non-BMP character not supported in Tk

Getting this error

Can you check https://stackoverflow.com/questions/32442608/ucs-2-codec-cant-encode-characters-in-position-1050-1050 if this is useful.

@sahar99
Copy link

sahar99 commented Jan 15, 2019

encode the text when printing

print(tweet.text.encode("utf-8"))

what if the print statement hold jason not a text? it didn't work like this print(status._json.encode("utf-8"))

@toktok911
Copy link

encode the text when printing
print(tweet.text.encode("utf-8"))

what if the print statement hold jason not a text? it didn't work like this print(status._json.encode("utf-8"))

Then simply dont print the json output (the returned tweets) to the console and avoid that as much as possible.
Instead, write those returned tweets of a json format into a text file for example using a for loop and the write methods of Python, so that you can examine the json output later.
If you, however, tried redirecting the console output to a text file using something like:
python tweets2000.py > output200.txt , it may still prompt the encoding error.

@ghost
Copy link

ghost commented Mar 27, 2019

Well the problem is not with python actually , I was getting a similar I did a simple fix in windows there is a way you can set the defaulting encoding to utf-8 like I did but this feature is only available as a beta in win 10 but it resolved the error for me

so the way to test if python renders the problematic character in python is to just enter interactive mode in python and just print('xxxxx') my bet is it will another thing is I am using vs code as my ide which sets the encoding to utf-8 for python by default

Hope that helps

@RAJANAGORI
Copy link

run chcp 65001 at first.

steps:

  1. chcp 65001
  2. python demo.py

Thanks.....for kick-ass solution dude

@yasinaziz
Copy link

image

@yasinaziz
Copy link

image

@yasinaziz
Copy link

any1 can help to finish it

@BhargavRajyagor
Copy link

Please hep me to solve this

' File "C:\ProgramData\Anaconda3\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 '\x87' in position 39: character maps to "]

Thanks

@yesdeepakmittal
Copy link

import io
file = io.open('twitter.csv', "w", encoding="utf-8")

@shraddhap238
Copy link

encode the text when printing

print(tweet.text.encode("utf-8"))

thank u so much I was getting this problem from 2 days I was not able to solve it but after trying your solution it worked thanks a lot

Konboi added a commit to launchableinc/cli that referenced this issue Jun 9, 2021
I can't replay this error but I realized this error might be happened only on Windows.
so I referred other same error llSourcell/twitter_sentiment_challenge#1
@me-suzy
Copy link

me-suzy commented Oct 13, 2022

# READ

def read_text_from_file(file_path):
    """
    This function returns the content of a file.
     file_path: the path to the file you want to read from
    """
    with open(file_path, encoding='utf8') as f:
        text = f.read()
        return text

# WRITE

def write_to_file(text, file_path, encoding='utf8'):
    """
    This function writes a text to a file.
     text: the text you want to write
     file_path: the path to the file you want to write to
    """
    with open(file_path, 'wb') as f:
        f.write(text.encode('utf-8', 'ignore'))

@sabbir2609
Copy link

chcp 65001

same problem

@s-ali-mi
Copy link

open('test.txt', 'w+', encoding="utf-8")

@Ghirati
Copy link

Ghirati commented Feb 11, 2023

with open('example.txt', '(r/ w / a)', encoding='utf-8') as f:

@evansmbithi
Copy link

Since Python 3.7 you can change the encoding of standard streams with reconfigure()
import sys
sys.stdout.reconfigure(encoding='utf-8')

confirm whether encoding has changed from cp1252 to utf-8
print(sys.stdout.encoding)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests