Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

From a file containing prime numbers to a list of integers on Python

In order to work out some asymptotic behavior on the topic of twin prime conjecture, I am required to take a raw file(.csv or .txt) and convert that data into a list in python where I could reach by pointing its index number.

That is, I have a big(~10 million numbers) list of prime numbers in .csv file, lets say that this is that list:

2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83

I am and trying to produce the following

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83]

in order to examine, ay the third element in the list, which is 5.

The approach I am taking is the following:

import sys
import csv

# The csv file might contain very huge fields, therefore increase the field_size_limit:

csv.field_size_limit(sys.maxsize)

with open('primes1.csv') as csvfile:
    reader = csv.reader(csvfile, delimiter=' ')

    output = []
    for i in reader:
        output.append(i)

Then, if printing,

for rows in output:
    print(rows)

I am getting

['2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83'].

How does one resolve this? Thank you very much.

>Solution :

Maybe this:

with open("primes1.csv", "r") as f:
    lst = [int(i) for i in f.read().split(",")]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading