First time here? Check out the FAQ!

Ask Your Question
1

Txt to CSV conversion

asked 4 years ago

nooniensoong97 gravatar image

updated 4 years ago

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)
Preview: (hide)

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 ( 4 years ago )

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 ( 4 years ago )

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 ( 4 years ago )

1 Answer

Sort by » oldest newest most voted
0

answered 4 years ago

Sébastien gravatar image

updated 4 years ago

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

Preview: (hide)
link

Comments

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

nooniensoong97 gravatar imagenooniensoong97 ( 4 years ago )

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: 4 years ago

Seen: 816 times

Last updated: Apr 17 '20