In [1]:
import csv,os
In [2]:
#Input the filepath to your csv file: i.e. "C:\\MyFolder\\MyCSV.csv"
#filename = "C:\\MyFolder\\MyCSV.csv"
#filename = os.path.join(os.path.dirname(__file__)+"\SampleData\TestCSV.csv")
response = input("Please Enter Location of Input CSV (""Example: C:\Test\TestCSV.csv)"": ")
In [3]:
#Depending on your data source, you may need to change the encoding(i.e. ('utf-8','utf-16')
infile = open(response, 'r', encoding=("iso-8859-15"))
#infile = open(response, 'r')
# Read the headers of the csv file
def file_headers(infile):
global fieldnames
reader = csv.DictReader(infile)
fieldnames = reader.fieldnames
print (fieldnames)
file_headers(infile)
firstdata = csv.DictReader(infile).fieldnames
#Read the length of the file
def file_length(infile):
for i, length in enumerate(infile):
pass
print (str(i + 3) + " Total lines and headers")
file_length(infile)
#os.system("Pause")
#exit()
In [4]:
yesno = input("Would you like a sample output CSV (Y/N)?: ")
In [10]:
while True:
yesChoice = set(['yes', 'y', 'Y', 'YES'])
noChoice = set(['no', 'n', 'N', 'NO'])
if yesno in yesChoice:
# create a csv with sample data of the headers and row
newcsv = input("Please Enter an Output Location and filename for Sample CSV: ")
with open (newcsv, 'w') as file:
writer = csv.writer(file,lineterminator="\n")
writer.writerow(fieldnames)
number = 50
for _ in range (number):
writer.writerow(firstdata)
break
if yesno in noChoice:
break
os.system("Pause")
Out[10]: