Read-Headers of a CSV File

If a file is too large to be opened by Excel or Notepad, this script will read the csv file and allow a user to output a sample csv


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


['MSG_TYPE', 'MMSI', 'NAME', 'IMO_NUMBER', 'CALL_SIGN', 'LAT_AVG', 'LON_AVG', 'PERIOD', 'SPEED_KNOTS', 'COG_DEG', 'HEADING_DEG', 'NAV_STATUS', 'NAV_SENSOR', 'SHIP_AND_CARGO_TYPE', 'DRAUGHT', 'DIM_BOW', 'DIM_STERN', 'DIM_PORT', 'DIM_STARBOARD', 'MMSI_COUNTRY_CD', 'RECEIVER']
9273629 Total lines and headers

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]:
0