Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read CSV file with ascii/ansi/unicode?

Status
Not open for further replies.

Guffy

Programmer
Jun 23, 2004
5
NL
Hi,

I have an csv file i want to read with VBScript. All works fine except something goes wrong with chars like éëü etc...
When i view the file with Total Commander and unicode+ascii selected these chars look allright, when i look with unicode+ansi i get: ‚‰

I have looked around and found i can open the file like this:
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const Unicode = -1, ASCII = 0
fso_OpenTextFile("file.csv", 1, 0, ASCII) or fso_OpenTextFile("file.csv", 1, 0, Unicode)
But both don't seem to work :(

the example file "file.csv" consists of only : ‚‰
Anyone a solution to read that file correctly?
 
Hello Guffy,

Most probably you have formatted file rather than a plain text file. Check again the source.

regards - tsuji
 
I get this file through other persons in the organisation so I can't do little about the source file.

Is there an other way to read the file correctly?

Total commander can 'read' (show) it correctly with unicode+ascii selected, but in VBScript this is not possible?
 
Guffy,

Run this for preliminary visual inspection to convince oneself that one has in hand a formatted file rather than a plain text.
Code:
filespec="d:\test\abc.cvs"   'edit this

set fso=createobject("scripting.filesystemobject")
set f=fso.getfile(filespec)
set ots=f.openastextstream(1,-1) 'unicode=-1, ascii=0
if f.size>500 then rsize=500 else rsize=f.size
s=escape(ots.read(rsize))
ots.close
set ots=nothing : set f=nothing : set fso=nothing
wscript.echo s
- tsuji
 
Ok i'm convinced, but what now?
Can I convert it to a plain text file?

I runned the code and get %u201A..etc.

 
Guffy,

Should your colleague or the party providing the .csv respect what .csv spec be? It should not be a formatted file. That's a sensible way to enforce standard.

- tsuji
 
Ok I think thats the best way too, but in case my colleague can't fix the problem (and i think this is going to be the case) there is no way in VBScript to convert it in some way?

I am now writing a function which does:
file=Replace(file,Chr(128),"Ç")
file=Replace(file,Chr(129),"ü")

:(
 
Guffy,

You have to know the regularity of the construction of the real info contained. Something in this direction is to eliminate all except a-z,A-Z,space(%20),comma(%2C),crlf(%0D%0A), tab, period,.... But you see the point. Just like doing some forensic! For one-off thing, it's fine. Else, it is plain unreasonable to ask somebody else to do this.

If you know how it is prepared, it would save you the trouble. For instance, if it is a win word doc, then reopen it with win word and save as a truly .csv---and this all!

- tsuji
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top