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.Sat, 18 Apr 2020 18:49:29 +0200Txt to CSV conversionhttps://ask.sagemath.org/question/50775/txt-to-csv-conversion/I would like to convert mathematical constants in txt format
to csv format where each digit is its own entry in the csv file.
Here is my code so far, which does not do what I want.
import csv
with open('constants/PiDec.txt','r') as rf:
with open('constants/PiDec.csv','a') as wf:
csv_writer = csv.writer(wf)
s_t_r = 1
i = 1
f_contents = rf.read(s_t_r)
while len(f_contents) > 0:
csv_writer.writerow(f_contents)
f_contents = rf.read(s_t_r)
rf.seek(i)
i = i+1
f_contents = rf.read(s_t_r)Thu, 16 Apr 2020 02:21:48 +0200https://ask.sagemath.org/question/50775/txt-to-csv-conversion/Comment by slelievre for <p>I would like to convert mathematical constants in txt format
to csv format where each digit is its own entry in the csv file.</p>
<p>Here is my code so far, which does not do what I want.</p>
<pre><code>import csv
with open('constants/PiDec.txt','r') as rf:
with open('constants/PiDec.csv','a') as wf:
csv_writer = csv.writer(wf)
s_t_r = 1
i = 1
f_contents = rf.read(s_t_r)
while len(f_contents) > 0:
csv_writer.writerow(f_contents)
f_contents = rf.read(s_t_r)
rf.seek(i)
i = i+1
f_contents = rf.read(s_t_r)
</code></pre>
https://ask.sagemath.org/question/50775/txt-to-csv-conversion/?comment=50781#post-id-50781Can you give a simplified small example of what `PiDec.txt` contains and what you want `PiDec.csv` to look like?Thu, 16 Apr 2020 07:53:15 +0200https://ask.sagemath.org/question/50775/txt-to-csv-conversion/?comment=50781#post-id-50781Comment by nooniensoong97 for <p>I would like to convert mathematical constants in txt format
to csv format where each digit is its own entry in the csv file.</p>
<p>Here is my code so far, which does not do what I want.</p>
<pre><code>import csv
with open('constants/PiDec.txt','r') as rf:
with open('constants/PiDec.csv','a') as wf:
csv_writer = csv.writer(wf)
s_t_r = 1
i = 1
f_contents = rf.read(s_t_r)
while len(f_contents) > 0:
csv_writer.writerow(f_contents)
f_contents = rf.read(s_t_r)
rf.seek(i)
i = i+1
f_contents = rf.read(s_t_r)
</code></pre>
https://ask.sagemath.org/question/50775/txt-to-csv-conversion/?comment=50805#post-id-50805I would eventually like to create a csv database with a bunch of mathematical constants that are all the way out to the millionth digit.Fri, 17 Apr 2020 03:32:01 +0200https://ask.sagemath.org/question/50775/txt-to-csv-conversion/?comment=50805#post-id-50805Comment by nooniensoong97 for <p>I would like to convert mathematical constants in txt format
to csv format where each digit is its own entry in the csv file.</p>
<p>Here is my code so far, which does not do what I want.</p>
<pre><code>import csv
with open('constants/PiDec.txt','r') as rf:
with open('constants/PiDec.csv','a') as wf:
csv_writer = csv.writer(wf)
s_t_r = 1
i = 1
f_contents = rf.read(s_t_r)
while len(f_contents) > 0:
csv_writer.writerow(f_contents)
f_contents = rf.read(s_t_r)
rf.seek(i)
i = i+1
f_contents = rf.read(s_t_r)
</code></pre>
https://ask.sagemath.org/question/50775/txt-to-csv-conversion/?comment=50803#post-id-50803First of all I can't call up the full txt. It is Pi to the millionth digit. So I have to call the individual digit, and place each individual digit in it's own cell.Fri, 17 Apr 2020 00:51:34 +0200https://ask.sagemath.org/question/50775/txt-to-csv-conversion/?comment=50803#post-id-50803Answer by Sébastien for <p>I would like to convert mathematical constants in txt format
to csv format where each digit is its own entry in the csv file.</p>
<p>Here is my code so far, which does not do what I want.</p>
<pre><code>import csv
with open('constants/PiDec.txt','r') as rf:
with open('constants/PiDec.csv','a') as wf:
csv_writer = csv.writer(wf)
s_t_r = 1
i = 1
f_contents = rf.read(s_t_r)
while len(f_contents) > 0:
csv_writer.writerow(f_contents)
f_contents = rf.read(s_t_r)
rf.seek(i)
i = i+1
f_contents = rf.read(s_t_r)
</code></pre>
https://ask.sagemath.org/question/50775/txt-to-csv-conversion/?answer=50783#post-id-50783I think it is better (and faster) to read the file once and for all and put all the content in one string `s`. Then, you don't need to seek to the good position which is certainly prone to errors:
with open('constants/PiDec.txt','r') as f:
s = f.read()
Note that you may also create 10^6 digits of pi with sage without reading it from a file, it takes 1 seconds on my computer and about 20s for 10^7 digits:
sage: %time s = str(pi.n(digits=10^6))
CPU times: user 1.18 s, sys: 22.9 ms, total: 1.2 s
Wall time: 1.2 s
sage: %time s = str(pi.n(digits=10^7))
CPU times: user 19.2 s, sys: 366 ms, total: 19.5 s
Wall time: 19.5 s
sage: len(s)
10000001
If I understand correctly your code and what you try to do with the seek calls, you want to put one digit in each line of the csv file, you don't need any comma in your comma separated file, so you don't really need to use `csv` library for that. Just a basic str join will do the trick.
result = '\n'.join(a for a in s)
with open('constants/PiDec.csv','w') as f:
f.write(result)
I don't know if there exists a PEP for that, but I would suggest to keep the content of the with statement to the strict necessary (I/O file operations).Thu, 16 Apr 2020 09:30:41 +0200https://ask.sagemath.org/question/50775/txt-to-csv-conversion/?answer=50783#post-id-50783Comment by nooniensoong97 for <p>I think it is better (and faster) to read the file once and for all and put all the content in one string <code>s</code>. Then, you don't need to seek to the good position which is certainly prone to errors:</p>
<pre><code>with open('constants/PiDec.txt','r') as f:
s = f.read()
</code></pre>
<p>Note that you may also create 10^6 digits of pi with sage without reading it from a file, it takes 1 seconds on my computer and about 20s for 10^7 digits:</p>
<pre><code>sage: %time s = str(pi.n(digits=10^6))
CPU times: user 1.18 s, sys: 22.9 ms, total: 1.2 s
Wall time: 1.2 s
sage: %time s = str(pi.n(digits=10^7))
CPU times: user 19.2 s, sys: 366 ms, total: 19.5 s
Wall time: 19.5 s
sage: len(s)
10000001
</code></pre>
<p>If I understand correctly your code and what you try to do with the seek calls, you want to put one digit in each line of the csv file, you don't need any comma in your comma separated file, so you don't really need to use <code>csv</code> library for that. Just a basic str join will do the trick.</p>
<pre><code>result = '\n'.join(a for a in s)
with open('constants/PiDec.csv','w') as f:
f.write(result)
</code></pre>
<p>I don't know if there exists a PEP for that, but I would suggest to keep the content of the with statement to the strict necessary (I/O file operations).</p>
https://ask.sagemath.org/question/50775/txt-to-csv-conversion/?comment=50843#post-id-50843Thanks for the input. I thought that the number was going to be too big for memory.Sat, 18 Apr 2020 18:49:29 +0200https://ask.sagemath.org/question/50775/txt-to-csv-conversion/?comment=50843#post-id-50843