Ask Your Question
1

Txt to CSV conversion

asked 2020-04-15 19:21:48 -0600

nooniensoong97 gravatar image

updated 2020-04-16 00:51:23 -0600

slelievre gravatar image

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)
edit retag flag offensive close merge delete

Comments

Can you give a simplified small example of what PiDec.txt contains and what you want PiDec.csv to look like?

slelievre gravatar imageslelievre ( 2020-04-16 00:53:15 -0600 )edit

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

nooniensoong97 gravatar imagenooniensoong97 ( 2020-04-16 17:51:34 -0600 )edit

I would eventually like to create a csv database with a bunch of mathematical constants that are all the way out to the millionth digit.

nooniensoong97 gravatar imagenooniensoong97 ( 2020-04-16 20:32:01 -0600 )edit

1 answer

Sort by » oldest newest most voted
0

answered 2020-04-16 02:30:41 -0600

Sébastien gravatar image

updated 2020-04-17 01:38:57 -0600

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

edit flag offensive delete link more

Comments

Thanks for the input. I thought that the number was going to be too big for memory.

nooniensoong97 gravatar imagenooniensoong97 ( 2020-04-18 11:49:29 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2020-04-15 19:21:48 -0600

Seen: 52 times

Last updated: Apr 17