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

1 sentence utf-8 detected as Windows-1252 #185

Open
EralpB opened this issue Dec 14, 2019 · 3 comments
Open

1 sentence utf-8 detected as Windows-1252 #185

EralpB opened this issue Dec 14, 2019 · 3 comments

Comments

@EralpB
Copy link

EralpB commented Dec 14, 2019

Thanks for coming up with this utility, it's a great need but this fails even with simplest examples I can't see how I'd trust this on production.

bs_2 = b'Carter\xe2\x80\x99s Janitorial'
chardet.detect(bs_2)
{'encoding': 'Windows-1252', 'confidence': 0.73, 'language': ''}
bs_2.decode('cp1252')
'Carter’s Janitorial'
bs_2.decode('utf-8')
'Carter’s Janitorial'

am I doing something wrong? it's just couple of characters and it's clearly utf-8.

@Ousret
Copy link

Ousret commented Jan 3, 2020

You can try https://github.com/Ousret/charset_normalizer :)

@yomajo
Copy link

yomajo commented Feb 12, 2020

I am working with CSV files currently and personally tried 2 detection libraries. Both failed to detect contents are windows...: cp1257

In some StackOverflow answers I found:

def get_encoding(csvfile_path):
with open(csvfile_path) as f:
print(f)

which outputs:
<_io.TextIOWrapper name='Output/Headlines_data.csv' mode='r' encoding='cp1257'>

So I wrote basic extractor myself, with a fallback to lib solution:
from re import findall
def get_encoding(csvfile_path):
with open(csvfile_path) as f:
raw_result = str(f)
try:
matches = findall(r"encoding='.+'>", raw_result)
encoding = matches[0].replace('encoding=\'', '').replace('\'>','')
return encoding
except:
return get_encoding_via_lib(csvfile_path)

I'm kinda new, sorry for indentation stuff, code block ignores my spaces...

@chomechome
Copy link

chomechome commented May 12, 2020

I also haven't been able to successfully use chardet in production. So, I've recently built a more accurate encoding detection library myself: https://github.com/chomechome/charamel

nijel added a commit to nijel/translate that referenced this issue Feb 12, 2022
This is better maintained and more reliable detection.

This avoids issues with chardet mistakenly reporting utf-8 content as
windows-1252, see chardet/chardet#185
nijel added a commit to nijel/translate that referenced this issue Feb 12, 2022
This is better maintained and more reliable detection.

This avoids issues with chardet mistakenly reporting utf-8 content as
windows-1252, see chardet/chardet#185
nijel added a commit to translate/translate that referenced this issue Feb 12, 2022
This is better maintained and more reliable detection.

This avoids issues with chardet mistakenly reporting utf-8 content as
windows-1252, see chardet/chardet#185
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

4 participants