Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3681392
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T03:41:03+00:00 2026-05-19T03:41:03+00:00

Working on importing a tab-delimited file over HTTP in Python. Before inserting a row’s

  • 0

Working on importing a tab-delimited file over HTTP in Python.

Before inserting a row’s data into MongoDB, I’m removing slashes, ticks and quotes from the string.

Whatever the encoding of the data is, MongoDB is throwing me the exception:

bson.errors.InvalidStringData: strings in documents must be valid UTF-8

So in an endeavour to solve this problem, from the reading I’ve done I want to as quickly as I can, convert the row’s data to Unicode using the unicode() function. In addition, I have tried calling the decode() function passing “unicode” as the first parameter but receive the error:

LookupError: unknown encoding: unicode

From there, I can make my string manipulations such as replacing the slashes, ticks, and quotes. Then before inserting the data into MongoDB, convert it to UTF-8 using the str.encode(‘utf-8’) function.

Problem: When converting to Unicode, I am receiving the error

UnicodeDecodeError: 'ascii' codec can't decode byte 0x93 in position 1258: ordinal not in range(128)

With this error, I’m not exactly sure where to continue.

My question is this: How do I successfully import the data from a file without knowing its encoding and successfully insert it into MongoDB, which requires UTF-8?

Thanks Much!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T03:41:03+00:00Added an answer on May 19, 2026 at 3:41 am

    Try these in order:

    (0) Check that your removal of the slashes/ticks/etc is not butchering the data. What’s a tick? Please show your code. Please show a sample of the raw data … use print repr(sample_raw data) and copy/paste the output into an edit of your question.

    (1) There’s an old maxim: “If the encoding of a file is unknown, or stated to be ISO-8859-1, it is cp1252” … where are you getting it from? If it’s coming from Western Europe, the Americas, or any English/French/Spanish-speaking country/territory elsewhere, and it’s not valid UTF-8, then it’s likely to be cp1252

    [Edit 2] Your error byte 0x93 decodes to U+201C LEFT DOUBLE QUOTATION MARK for all encodings cp1250 to cp1258 inclusive … what language is the text written in? [/Edit 2]

    (2) Save the file (before tick removal), then open the file in your browser: Does it look sensible? What do you see when you click on View / Character Encoding?

    (3) Try chardet

    Edit with some more advice:

    Once you know what the encoding is (let’s assume it’s cp1252):

    (1) convert your input data to unicode: uc = raw_data.decode('cp1252')

    (2) process the data (remove slashes/ticks/etc) as unicode: clean_uc = manipulate(uc)

    (3) you need to output your data encoded as utf8: to_mongo = clean_uc.encode('utf8')

    Note 1: Your error message says “can’t decode byte 0x93 in position 1258” … 1258 bytes is a rather long chunk of text; is this reasonable? Have you had a look at the data that it is complaining about? How? what did you see?

    Note 2: Please consider reading the Python Unicode HOWTO and this article

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working with FMOD in C#. I've tried importing the fmodex.dll file by selecting
I'm working on a script for importing component information for SketchUp. A very helpful
Currently I am working with Oracle identity federation 10.1.4.0.1. I am facing one problem
I'm doing some content importing using the node import module in drupal. My problem
I guess I haven't really had to do this much before because I am
Working on a end of the year project for school and chose to create
Working with some basic java apps on CentOS 5 linux and I have my
I am trying to make a similar bit of code like at the bottom
I am using Eclipse Helios* with PDT, and when I am editing JavaScript files
I'm trying to protect page by making a member only area the code I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.