# Revision history [back]

I 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:


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).

I 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:


Note that you may 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).

I 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:


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).