2
import csv
import random
from faker import Faker
from datetime import datetime


l=Faker('en_GB') 
f=open("test.csv","r")
k=csv.reader(f)

g=open("1.csv","w")
w=csv.writer(g)
w.writerow(('id','name','address','college','company','dob','age'))
for i in range(20000):

    w.writerow((i+1,l.name(),l.address(),random.choice(['psg','sona','amirta','anna university']),random.choice(['CTS','INFY','HTC']),(random.randrange(1950,1995,1),random.randrange(1,13,1),random.randrange(1,32,1)),random.choice(range(0,100))))
f.close()

when i increase the range to 10000000 terminal kills the process...... please can anyone help me . how can i generate larger csv file with random data .

2
  • What do you mean the terminal kills the process? It is automatically being killed or you are killing it? Commented Jul 17, 2015 at 11:22
  • automatically killed by kernel Commented Jul 17, 2015 at 16:10

2 Answers 2

2

If you're using Python 2, range will create a list and may therefore have memory troubles with large input values.

If that's the case, use xrange instead. It has fixed memory requirements regardless of the input value.

The Python 3 range is more akin to the Python 2 xrange so it shouldn't be a problem there.

Sign up to request clarification or add additional context in comments.

Comments

0
import csv
import random
from faker import Faker
from datetime import datetime


l=Faker('en_GB') 
f=open("test.csv","r")
k=csv.reader(f)

g=open("1.csv","a")
w=csv.writer(g)
w.writerow(('id','name','address','college','company','dob','age'))
for i in range(1000000):

    w.writerow((i+1,l.name(),l.address(),random.choice(['psg','sona','amirta','anna university']),random.choice(['CTS','INFY','HTC']),(random.randrange(1950,1995,1),random.randrange(1,13,1),random.randrange(1,32,1)),random.choice(range(0,100))))
f.close()

append option solved my problem.

g=open("1.csv","a") 

after i use this its taking time to create the csv file of 1.6 gb.with random data

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.