| 1 | initial version |
First, you can put all your lines into a list:
sage: with open('/path/to/your/file.txt', 'r') as f:
....: L = f.readlines()
So, then L is a list ot lines of the file
sage: L
['2.14\n', '3.15\n', '7.8 \n']
As you can see, the entries are strings, with maybe spaces and newlines at the end. You can clean such a strin with the strip() method:
sage: [l.strip() for l in L]
['2.14', '3.15', '7.8']
But you want Sage floating point numbers, not strings representing them, so you can do:
sage: [RDF(l.strip()) for l in L]
[2.14, 3.15, 7.8]
If you want to summarize this, you can do:
sage: with open('/tmp/file.txt', 'r') as f:
....: L = [RDF(l.strip()) for l in f.readlines()]
sage: L
[2.14, 3.15, 7.8]
| 2 | No.2 Revision |
First, you can put all your lines into a Python list:
sage: with open('/path/to/your/file.txt', 'r') as f:
....: L = f.readlines()
So, then L is a list ot lines of the filefile:
sage: L
['2.14\n', '3.15\n', '7.8 \n']
As you can see, the entries are strings, with maybe spaces and newlines at the end. You can clean such a strin string with the strip() method:
sage: [l.strip() for l in L]
['2.14', '3.15', '7.8']
But you want Sage floating point numbers, not strings representing them, so you can do:transform the string into elements of `RDF the real double field:
sage: [RDF(l.strip()) for l in L]
[2.14, 3.15, 7.8]
If you want to summarize this, you can directly do:
sage: with open('/tmp/file.txt', 'r') as f:
....: L = [RDF(l.strip()) for l in f.readlines()]
sage: L
[2.14, 3.15, 7.8]
| 3 | No.3 Revision |
First, you can put all your lines into a Python list:
sage: with open('/path/to/your/file.txt', 'r') as f:
....: L = f.readlines()
So, L is a list ot lines of the file:
sage: L
['2.14\n', '3.15\n', '7.8 \n']
As you can see, the entries are strings, with maybe spaces and newlines at the end. You can clean such a string with the strip() method:
sage: [l.strip() for l in L]
['2.14', '3.15', '7.8']
But you want Sage floating point numbers, not strings representing them, so you can transform the string into elements of `RDF RDF, the real double field:
sage: [RDF(l.strip()) for l in L]
[2.14, 3.15, 7.8]
If you want to summarize this, you can directly do:
sage: with open('/tmp/file.txt', 'r') as f:
....: L = [RDF(l.strip()) for l in f.readlines()]
sage: L
[2.14, 3.15, 7.8]
| 4 | No.4 Revision |
First, you can put all your lines into a Python list:
sage: with open('/path/to/your/file.txt', 'r') as f:
....: L = f.readlines()
So, L is a list ot lines of the file:
sage: L
['2.14\n', '3.15\n', '7.8 \n']
As you can see, the entries are strings, with maybe spaces and newlines at the end. You can clean such a string with the strip() method:
sage: [l.strip() for l in L]
['2.14', '3.15', '7.8']
But you want Sage floating point numbers, not strings representing them, so you can transform the string into elements of RDF, the real double field:
sage: [RDF(l.strip()) for l in L]
[2.14, 3.15, 7.8]
If you want to summarize this, you can directly do:
sage: with open('/tmp/file.txt', open('/path/to/your/file.txt', 'r') as f:
....: L = [RDF(l.strip()) for l in f.readlines()]
sage: L
[2.14, 3.15, 7.8]
| 5 | No.5 Revision |
First, you can put all your lines into a Python list:
sage: with open('/path/to/your/file.txt', 'r') as f:
....: L = f.readlines()
So, L is a list ot lines of the file:
sage: L
['2.14\n', '3.15\n', '7.8 \n']
As you can see, the entries are strings, with maybe spaces and newlines at the end. You can clean such a string with the strip() method:
sage: [l.strip() for l in L]
['2.14', '3.15', '7.8']
But you want Sage floating point numbers, not strings representing them, so you can transform the string into elements of RDF, the real double field:
sage: [RDF(l.strip()) for l in L]
[2.14, 3.15, 7.8]
If you want to summarize this, you can directly do:
sage: with open('/path/to/your/file.txt', 'r') as f:
....: L = [RDF(l.strip()) for l in f.readlines()]
sage: L
[2.14, 3.15, 7.8]
EDIT: we can get the best of our answer and the one of @niles as follows:
sage: with open('/path/to/your/file.txt', 'r') as f:
....: L = [RDF(l.strip()) for l in f]
| 6 | No.6 Revision |
First, you can put all your lines into a Python list:
sage: with open('/path/to/your/file.txt', 'r') as f:
....: L = f.readlines()
So, L is a list ot lines of the file:
sage: L
['2.14\n', '3.15\n', '7.8 \n']
As you can see, the entries are strings, with maybe spaces and newlines at the end. You can clean such a string with the strip() method:
sage: [l.strip() for l in L]
['2.14', '3.15', '7.8']
But you want Sage floating point numbers, not strings representing them, so you can transform the string into elements of RDF, the real double field:
sage: [RDF(l.strip()) for l in L]
[2.14, 3.15, 7.8]
If you want to summarize this, you can directly do:
sage: with open('/path/to/your/file.txt', 'r') as f:
....: L = [RDF(l.strip()) for l in f.readlines()]
sage: L
[2.14, 3.15, 7.8]
EDITUPDATE: we can get the best of our this answer and the one of @niles as follows:
sage: with open('/path/to/your/file.txt', 'r') as f:
....: L = [RDF(l.strip()) for l in f]
| 7 | No.7 Revision |
First, you can put all your lines into a Python list:
sage: with open('/path/to/your/file.txt', 'r') as f:
....: L = f.readlines()
So, L is a list ot lines of the file:
sage: L
['2.14\n', '3.15\n', '7.8 \n']
As you can see, the entries are strings, with maybe spaces and newlines at the end. You can clean such a string with the strip() method:
sage: [l.strip() for l in L]
['2.14', '3.15', '7.8']
But you want Sage floating point numbers, not strings representing them, so you can transform the string into elements of RDF, the real double field:
sage: [RDF(l.strip()) for l in L]
[2.14, 3.15, 7.8]
If you want to summarize this, you can directly do:
sage: with open('/path/to/your/file.txt', 'r') as f:
....: L = [RDF(l.strip()) for l in f.readlines()]
sage: L
[2.14, 3.15, 7.8]
UPDATE: we can get the best of this answer and the one of @niles @nbruin as follows:
sage: with open('/path/to/your/file.txt', 'r') as f:
....: L = [RDF(l.strip()) for l in f]
Copyright Sage, 2010. Some rights reserved under creative commons license. Content on this site is licensed under a Creative Commons Attribution Share Alike 3.0 license.