ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sun, 16 May 2021 22:16:16 +0200How to copy from a text filehttps://ask.sagemath.org/question/57141/how-to-copy-from-a-text-file/I have a file `abc.txt` containing:
0,1,0,1,1,...,1
I want to store these numbers in an array.
This code is not working:
f = open('path/abc.txt')
L = f.readlines()
for i in range(len(L)):
A.append(L[i])
Please help me.Sun, 16 May 2021 21:59:42 +0200https://ask.sagemath.org/question/57141/how-to-copy-from-a-text-file/Comment by tmonteil for <p>I have a file <code>abc.txt</code> containing:</p>
<pre><code>0,1,0,1,1,...,1
</code></pre>
<p>I want to store these numbers in an array.</p>
<p>This code is not working:</p>
<pre><code>f = open('path/abc.txt')
L = f.readlines()
for i in range(len(L)):
A.append(L[i])
</code></pre>
<p>Please help me.</p>
https://ask.sagemath.org/question/57141/how-to-copy-from-a-text-file/?comment=57142#post-id-57142What error do you get ?Sun, 16 May 2021 22:04:02 +0200https://ask.sagemath.org/question/57141/how-to-copy-from-a-text-file/?comment=57142#post-id-57142Answer by tmonteil for <p>I have a file <code>abc.txt</code> containing:</p>
<pre><code>0,1,0,1,1,...,1
</code></pre>
<p>I want to store these numbers in an array.</p>
<p>This code is not working:</p>
<pre><code>f = open('path/abc.txt')
L = f.readlines()
for i in range(len(L)):
A.append(L[i])
</code></pre>
<p>Please help me.</p>
https://ask.sagemath.org/question/57141/how-to-copy-from-a-text-file/?answer=57143#post-id-57143Assuming there is no problem with opening the file, here are some hints.
Your list `L` contains the lines of the file `f`, which seems to be only one line. Hence `L[0]` is a string that looks like `"0,1,0,1,1,....,1\n"` with `\n` standing for the line feed character.
To extract the elements of that list, remove that character, then split the string to get a list:
sage: L[0].replace('\n','').split(',')
['0', '1', '0', '1', '1',..., '1']
Then turn that list of strings into a list of integers:
sage: A = [ZZ(i) for i in L[0].replace('\n','').split(',')]
sage: A
[0, 1, 0, 1, 1, ..., 1]
Another (simpler) way is to use the `csv` module that deals well with comma separated values:
sage: r = csv.reader(f)
sage: A = [ZZ(i) for i in next(r)]
sage: A
[0, 1, 0, 1, 1, ..., 1]
Finally, the [Python documentation about reading and writing files](https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files)
recommends using `with` to deal with files.
So instead of
sage: f = open('path/abc.txt')
sage: L = f.readlines()
sage: A = [ZZ(i) for i in L[0].replace('\n','').split(',')]
the advice is to use
sage: with open('path/abc.txt', 'r') as f:
....: L = f.readlines()
sage: A = [ZZ(i) for i in L[0].replace('\n','').split(',')]
This takes care of closing the file after it has been used,
including if some error happens while reading from the file.Sun, 16 May 2021 22:16:16 +0200https://ask.sagemath.org/question/57141/how-to-copy-from-a-text-file/?answer=57143#post-id-57143