csStudent2102
csStudent2102

Reputation: 75

How to generate a text file in a for loop?

I have a csv file with Names, Email addresses and Total Marks and, using Python, I am attempting to generate a text file for each person where the name of the text file is (email).txt. How do I loop through row in the csv file, obtain the email address of each person and generate a text file in the format (email).txt to my directory?

import pandas as pd

reader=pd.read_csv('Marks.csv', delimiter = ',')
reader.head()
email = reader['EMAIL']
print(email)
for email in row:
   print(email)

Upvotes: 1

Views: 728

Answers (1)

ksohan
ksohan

Reputation: 1203

You need to use open function to create the files

import pandas as pd

reader=pd.read_csv('Marks.csv', delimiter = ',')
reader.head()
email = reader['EMAIL']
print(email)
for email in row:
   filename=email+".txt"
   open(filename,"w") # This line will create a file named email.txt
   print(email)

Upvotes: 1

Related Questions