[sage-support] Re: Reading numbers from a file
It is no question of format... simply the line length which can be megabytes.. But Burcin pointed out the solution : http://stackoverflow.com/questions/417703/python-c-like-stream-input One suggestion, though, wouldn't you feel this should be readily available from sage ? Or at list some function like Mathematica ReadList which can read simple numbers (the csv module almost does it !) Anyhow, thanks to all ! Nicolas On 4 juin, 10:31, Craig Citro wrote: > > This is almost what I want to do. I had figured out that trick but my > > problem is that the line that is input is, in my case, really long and > > gulps a lot of memory. > > > Thus, to save memory, I need to read the numbers one by one (or a > > small bunch of them at a time). It seems I have to read line by line, > > which in my case of a really long line, is not easy. > > > Thanks anyhow ! > > I guess I don't understand what the format of your file is. Can you > describe it a little more explicitly? Are you saying that there are > multiple real numbers per line? (Python could easily handle that case, > just use s.split(',') on each line.) Or are you saying the lines are > so long that just reading them into memory is a slowdown? How long is > one of these lines? > > -cc --~--~-~--~~~---~--~~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://www.sagemath.org -~--~~~~--~~--~--~---
[sage-support] Re: Reading numbers from a file
On Thu, 4 Jun 2009 01:21:06 -0700 (PDT) Nicolas wrote: > > This is almost what I want to do. I had figured out that trick but my > problem is that the line that is input is, in my case, really long and > gulps a lot of memory. > > Thus, to save memory, I need to read the numbers one by one (or a > small bunch of them at a time). It seems I have to read line by line, > which in my case of a really long line, is not easy. > > Thanks anyhow ! See here: http://stackoverflow.com/questions/417703/python-c-like-stream-input Cheers, Burcin --~--~-~--~~~---~--~~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://www.sagemath.org -~--~~~~--~~--~--~---
[sage-support] Re: Reading numbers from a file
This is almost what I want to do. I had figured out that trick but my problem is that the line that is input is, in my case, really long and gulps a lot of memory. Thus, to save memory, I need to read the numbers one by one (or a small bunch of them at a time). It seems I have to read line by line, which in my case of a really long line, is not easy. Thanks anyhow ! On 4 juin, 10:14, Craig Citro wrote: > > In short, anyone knows a simple trick provided by sage or python to > > read numbers from a file without redoing the parsing stuff ? > > Luckily, both python's float type and Sage's RealDoubleField (or any > of the RealFields) are smart enough to convert from strings: > > [craigci...@sharma ~/temp] $ cat reals.txt > 3.14159 > 4 > 3.0e17 > -5 > > [craigci...@sharma ~/temp] $ sage > -- > | Sage Version 4.0, Release Date: 2009-05-29 | > | Type notebook() for the GUI, and license() for information. | > -- > > sage: f = open('reals.txt') > sage: [ RDF(x) for x in f.readlines() ] > [3.14159, 4.0, 3e+17, -5.0] > sage: f.close() > > sage: f = open('reals.txt') > sage: [ float(x) for x in f.readlines() ] > [3.14158999, 4.0, 3e+17, -5.0] > sage: f.close() > > Is that what you were looking to do? Or are your files of real numbers > formatted differently? > > -cc --~--~-~--~~~---~--~~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://www.sagemath.org -~--~~~~--~~--~--~---
[sage-support] Re: Reading numbers from a file
> This is almost what I want to do. I had figured out that trick but my > problem is that the line that is input is, in my case, really long and > gulps a lot of memory. > > Thus, to save memory, I need to read the numbers one by one (or a > small bunch of them at a time). It seems I have to read line by line, > which in my case of a really long line, is not easy. > > Thanks anyhow ! > I guess I don't understand what the format of your file is. Can you describe it a little more explicitly? Are you saying that there are multiple real numbers per line? (Python could easily handle that case, just use s.split(',') on each line.) Or are you saying the lines are so long that just reading them into memory is a slowdown? How long is one of these lines? -cc --~--~-~--~~~---~--~~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://www.sagemath.org -~--~~~~--~~--~--~---
[sage-support] Re: Reading numbers from a file
> In short, anyone knows a simple trick provided by sage or python to > read numbers from a file without redoing the parsing stuff ? > Luckily, both python's float type and Sage's RealDoubleField (or any of the RealFields) are smart enough to convert from strings: [craigci...@sharma ~/temp] $ cat reals.txt 3.14159 4 3.0e17 -5 [craigci...@sharma ~/temp] $ sage -- | Sage Version 4.0, Release Date: 2009-05-29 | | Type notebook() for the GUI, and license() for information.| -- sage: f = open('reals.txt') sage: [ RDF(x) for x in f.readlines() ] [3.14159, 4.0, 3e+17, -5.0] sage: f.close() sage: f = open('reals.txt') sage: [ float(x) for x in f.readlines() ] [3.14158999, 4.0, 3e+17, -5.0] sage: f.close() Is that what you were looking to do? Or are your files of real numbers formatted differently? -cc --~--~-~--~~~---~--~~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://www.sagemath.org -~--~~~~--~~--~--~---