Re: Different ways of reading numbers in text files - which recommended?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



On 5 elo, 17:31, "Dave O." <nob...@xxxxxxxxxxx> wrote:
Not enough information.

But I give you what I can - Firstly the most important aspect is how are you
reading the file, the fastest method is to read the whole file into a string
or byte array in a single pass.

Speed is not important. It is more important to do it in a simple way.
I am open to all alternatives. This is a part of my question.

Are the files fixed column or TAB separated or some other delimiter?

They are written by an old VB6 program, so they will have varying
amounts of space between the fields of varying (and unknown) sizes,
except for their type.

Assuming you've read the whole file into a single string the next step is to
use the Split function to separate the file into an array of lines, that is:
split on vbCrLf

then you need to work on each line, if it's fixed column you'd use Mid to
get each element then trim off any trailing spaces then if necessary convert
to a number.

I thought this would be an inefficient and complicated way. Sometimes
there are spaces between words in a quoted string.

If it's TAB separated then the easiest way is to use Split again, this time
with vbTab as the delimiter.

Another possibility if the fields are fixed length is to make a user defined

They are of varying sizes, because 2.000 will be written as 2. while
1.3456567 may be written as such.

Type with each element the correct length and then read the file record by
record, this will be slower to read but quicker to process each line. I've
no idea which method would be faster overall, it rather depends on too many
factors, most of which I don't have.

Speed is the least important issue. I would prefer not to spend time
and energy doing parsing of strings to convert a text file into
numbers. In Fortran, four decades ago, we used to be able to read
several numbers of varying string lengths in a single command. Now it
looks as if we are going backwards.

Thanks for your comments.


Regards
Dave O.

"C" <wrong.addres...@xxxxxxxxx> wrote in message

news:2501f03c-07f0-4f2d-94c9-61caadafba2d@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

There seem to be several ways of reading files, and even more ways to
read files full of numbers (integer and floating) and strings. Could
someone advise me on which method is recommended? The file to be read
could look like this. The sequence is of course known, i.e., first two
are integers, then a floating point number, then a string, etc.

12     0          4.334E-12    effect
3.36755      "temperature at the top"
3                        55
2 331
0.4326765 0.242 M

.



Relevant Pages

  • Re: Elementary string-parsing
    ... "return" and passing arguments). ... stack at the end; hence they appear sort of global. ... "ValueError" if the string isn't a representation of a number? ... Convert a string or a number to floating point. ...
    (comp.lang.python)
  • Re: Request for comments for my parse-float
    ... parse floating-point numbers from a string, ... fed into read-from-string. ... This is ok if you want to restrict the syntax for floating point ... (defun parse-float (str) ...
    (comp.lang.lisp)
  • Re: How to convert float to sortable integer in Python
    ... it convert ieee 754 floating point number to ... and than convert the integer to sortable string? ... prev, curr = 1.0, 0.5 ...
    (comp.lang.python)
  • Re: Big numbers
    ... The exact value represented by that floating point literal in the ... closest that 'double' can get to representing that particular number ... and perform the usual unsigned overflow mechanisms, ... Alternatively the input mechanism will accept a string, ...
    (comp.lang.c)
  • Re: Precision issue
    ... > I know this is an FAQ, but the one thing I've never seen explained ... > hold for floating point numbers, and that holds just as true for the short ... > first trying a general format with 15 digits of precision then parsing that ... The string is a stark data structure and everywhere it is ...
    (comp.lang.python)