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

  • Home
  • SEARCH
  • 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 4000042
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:43:58+00:00 2026-05-20T07:43:58+00:00

I’m writing a set of tool in python to extract data from some xml

  • 0

I’m writing a set of tool in python to extract data from some xml files that are generated by a traffic simulation software. As the resulting files can be quite big I use the xml.parsers.expat to parse them.

The issue is, when I run my scripts at work on a Windows XP machine it work perfectly but at home, on Ubuntu 10.10, on the very same file I get the following error :
ExpatError: not well-formed (invalid token): line 1, column 0

The file was originally encoded in utf-8 and the encoding declared in the tag was ascii so try to change it to utf-8 (or UTF8 or utf8) without success. As the BOM was absent I tryed to write it, still without success. I also tried to replace Windows line break (CR/LF) by Unix ones (CR).Without any success too.

Also the python’s version at work is 2.7.1, on my Ubuntu box it’s 2.6.6, but don’t think my issue is related that : I upgraded my work computer’s Python from 2.6 to 2.7 a few weeks ago without trouble.

As I’m not an expert here, I’m running out of idea, any hint ?

Edit:
After further investigation (I got an headache now, I hate Unicode related trouble) it look like the issue was solved by setting properly the system environment variable LANG, LC_ALL and LANGUAGE to (in my case) “fr_FR.utf-8”. I don’t understand why they weren’t at first neither why now, it work…

I thank you guys for the hand !

  • 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-20T07:43:58+00:00Added an answer on May 20, 2026 at 7:43 am

    Excerpts from the documentation:

    xml.parsers.expat.XML_ERROR_INVALID_TOKEN
    Raised when an input byte could not properly be assigned to a character; for example, a NUL byte (value 0) in a UTF-8 input stream.

    ExpatError.lineno
    Line number on which the error was detected. The first line is numbered 1.

    ExpatError.offset
    Character offset into the line where the error occurred. The first column is numbered 0.

    The above tends to indicate that you have a problem with the very first byte in your file.

    Start with the original file, the one that worked on Windows. Edit your question to show the results of doing this:

    python -c "print repr(open('win_ok_file.xml', 'rb').read(200))"
    

    which will show unambiguously what is in the first 200 bytes in your file.

    Also show us a cut-down version of your code that you have checked will work on Windows to get past the initial error, but reproduces the problem on Linux.

    Some assertions, for what they are worth:

    • “The file was originally encoded in
      utf-8 and the encoding declared in
      the tag was ascii” … If the
      encoding in the XML declaration is
      “ascii” but there are non-ASCII
      characters in the file, complying
      parsers should raise an exception.
      Are you sure of what you report?

    • The default encoding for XML
      documents is UTF-8. In other words,
      if the encoding is not mentioned in
      the XML declaration, or there is no
      XML declaration at all, the parser is
      required to decode using UTF-8.

    • Putting a UTF-8 BOM at the start is
      more likely to hinder than help.

    • The XML standard requires parsers to
      accept CR as a valid byte in an XML
      document and then immediately pretend
      it didn’t exist (except maybe in an
      element with
      xmlns:space="preserve"). Changing
      CR LF to LF is not a good idea.

    And some questions: How many bytes in a “quite big” file? Have you considered using iterparse() from xml.etree.cElementTree or lxml?

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a French site that I want to parse, but am running into
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

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.