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 4622788
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:53:35+00:00 2026-05-22T02:53:35+00:00

I am trying to read text files with Ruby 1.9 and convert them into

  • 0

I am trying to read text files with Ruby 1.9 and convert them into my own XML structure. I don’t have control over the source text file so they could be in any encoding.

Here is what I do at the moment:

lines = File.readlines(input_file)
lines.each do |line|
  #do something
end

I have a problem with a file that contains the é character (xE9). When I try to process the corresponding line I get a Invalid byte sequence in UTF-8 exception when I call .match(...) on the string.

I tried to use the workaround described at Fixing invalid UTF-8 in Ruby, revisited

lines = File.readlines(input_file)
ic = Iconv.new('UTF-8//IGNORE', 'UTF-8')
lines.each do |line|
  unless line.empty?
   valid_string = ic.iconv(line + ' ')[0..-2]
   #do something
  end
end

but this simply strips the é character from the line which is not what I want.

I think the real problem is that the file itself doesn’t seem to be in UTF-8 but uses some ANSI encoding. Although the file is not UTF-8 the resulting line object says it is UTF-8 when calling .encoding; My guess is that I need to use a different way to read the file so that it works for both ANSI and UTF-8 files but I am a Ruby beginner and I really don’t know where to start.

  • 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-22T02:53:35+00:00Added an answer on May 22, 2026 at 2:53 am

    The character is part of the ISO-8859-1 and Win-1252 character sets, among others. The second is probably the most popular character set for Windows, and is your most likely source.

    RUBY_VERSION # => "1.9.2"
    

    That’s my Ruby version running the following tests. Note that in the following samples the # encoding lines aren’t comments, they’re directives to Ruby on which character set to use when unencoded binary characters are found:

    # encoding: Windows-1252
    
    RUBY_VERSION # => "1.9.2"
    
    asdf = "\xe9"
    asdf.encoding # => #<Encoding:Windows-1252>
    asdf.encode('UTF-8') # => "é"
    asdf.encode('UTF-8').encoding # => #<Encoding:UTF-8>
    

    This shows the character in ISO-8859-1:

    # encoding: ISO-8859-1
    
    RUBY_VERSION # => "1.9.2"
    
    asdf = "\xe9"
    asdf.encoding # => #<Encoding:ISO-8859-1>
    asdf.encode('UTF-8') # => "é"
    asdf.encode('UTF-8').encoding # => #<Encoding:UTF-8>
    

    James Gray did a series of articles a couple years ago about dealing with this stuff. It’s good reading.

    Now, back to trying to figure out what character set a character could be in: When you only have one character, because it could be in several sets at once, it is difficult to determine which set it is. If you have more characters >= “\x80” then you can run through the characters sets iconv support and try converting them. That’s messy, but I had to do that in Perl for some screen scraping about five years ago. An alternative is to use the Python chardet code.

    James Gray’s articles have a link to an article recommending rchardet.

    The above routines mention Mozilla’s Charset Detectors, which will give you more info on dealing with this.

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

Sidebar

Related Questions

I'm trying to convert Morse code into text. I have two text files to
I'm trying to read a text file that contains integers and store them into
I'm trying to have my application read through a text file and look for
I have some text files which I am trying to transform with a Perl
I am trying to read product information from some text files. In my text
I have an application that imports data read from text files from a directory
I'm trying to read remote text files using this code: function defdate(ipaddress) deffilePath =
I'm trying to save variables into text files and the Czech typographic rules drives
I have a string that appears in my text files. I am trying to
I am trying to read some text files, where each line needs to be

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.