Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Question on reading text files in Windows

Reply
Thread Tools

Question on reading text files in Windows

 
 
Jim Knowlton
Guest
Posts: n/a
 
      02-02-2009
I am running Ruby 1.86 on Windows, and having trouble reading in some
text files. For some text files, if I do something simple like:

myfile = File.open("logfile.log")
contents = myfile.read()
puts contents

I get each character seperated by a space, such as:

= = = V e r b o s e l o g g i n g s t a r t e d : 1 / 2 8 / 2
0 0 9
1 3 : 4 5 : 0 6 B u i l d t y p e : S H I P U N I C O D E

If I bring up the file in even a bare-bones editor (such as VIM), I
get the file as it normally is (without any extraneous spaces). Does
anyone know why this would be, or how I can work around it? It's
causing issues as I am trying to write a script to search for a
particular string of text, and obviously it isn't found, even though
it should be.

Thanks,

Jim
 
Reply With Quote
 
 
 
 
Stefan Lang
Guest
Posts: n/a
 
      02-02-2009
2009/2/2 Jim Knowlton <(E-Mail Removed)>:
> I am running Ruby 1.86 on Windows, and having trouble reading in some
> text files. For some text files, if I do something simple like:
>
> myfile =3D File.open("logfile.log")
> contents =3D myfile.read()
> puts contents
>
> I get each character seperated by a space, such as:
>
> =94=3D =3D =3D V e r b o s e l o g g i n g s t a r t e d : 1 / 2 =

8 / 2
> 0 0 9
> 1 3 : 4 5 : 0 6 B u i l d t y p e : S H I P U N I C O D E
>
> If I bring up the file in even a bare-bones editor (such as VIM), I
> get the file as it normally is (without any extraneous spaces). Does
> anyone know why this would be, or how I can work around it? It's
> causing issues as I am trying to write a script to search for a
> particular string of text, and obviously it isn't found, even though
> it should be.


The file is probably UTF-16 encoded and starts with a BOM.
Try to convert the string to UTF-8, or switch to Ruby 1.9.

Stefan

 
Reply With Quote
 
 
 
 
Stefan Lang
Guest
Posts: n/a
 
      02-02-2009
2009/2/2 Stefan Lang <(E-Mail Removed)>:
> 2009/2/2 Jim Knowlton <(E-Mail Removed)>:
>> I am running Ruby 1.86 on Windows, and having trouble reading in some
>> text files. For some text files, if I do something simple like:
>>
>> myfile =3D File.open("logfile.log")
>> contents =3D myfile.read()
>> puts contents
>>
>> I get each character seperated by a space, such as:
>>
>> =94=3D =3D =3D V e r b o s e l o g g i n g s t a r t e d : 1 / 2=

8 / 2
>> 0 0 9
>> 1 3 : 4 5 : 0 6 B u i l d t y p e : S H I P U N I C O D E
>>
>> If I bring up the file in even a bare-bones editor (such as VIM), I
>> get the file as it normally is (without any extraneous spaces). Does
>> anyone know why this would be, or how I can work around it? It's
>> causing issues as I am trying to write a script to search for a
>> particular string of text, and obviously it isn't found, even though
>> it should be.

>
> The file is probably UTF-16 encoded and starts with a BOM.
> Try to convert the string to UTF-8, or switch to Ruby 1.9.


Sorry, I meant to say "Try to convert the string to UTF-8 WITH Iconv"

Stefan

 
Reply With Quote
 
Jim Knowlton
Guest
Posts: n/a
 
      02-02-2009
Thanks for the pointer! I actually ended up using the iconv module,
and it worked like a charm. Incidentally, in case anyone else is
curious about this, Windows .REG files get saved as UTF-16 by default.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
UnauthorizedAccessException when reading XML files (no problem when reading other file-types) blabla120@gmx.net ASP .Net 0 09-15-2006 02:08 PM
how i can extract text from the PDF files,power point files,Ms word files? crazyprakash Java 4 10-30-2005 10:17 AM
Text files read multiple files into single file, and then recreate the multiple files googlinggoogler@hotmail.com Python 4 02-13-2005 05:44 PM
Question about reading a big binary file and write it into several text (ascii) files Albert Tu Python 2 01-25-2005 03:56 AM
reading the DB vs. reading a text file...performance preference? Darrel ASP .Net 3 11-11-2004 02:27 PM



Advertisments