
# Read a TAB delimited file with the csv module

# Data:
#   6/20/2014 AAPL 90.91
#   6/20/2014 MSFT 41.68
#   6/20/2014 FB 64.5
#   6/19/2014 AAPL 91.86
#   6/19/2014 MSFT 41.51
#   6/19/2014 FB 64.34


import csv

with open('inp-TAB.txt') as f:

    # Create a CSV "reader" (parser)
    reader = csv.reader(f, delimiter='\t')

    # Read each row with the CSV "reader"
    for row in reader:
        # row is a list
        date = row[0]
        symbol = row[1]
        closing_price = float(row[2])
        print(date, symbol, closing_price)

print()

# ###################################
# CSV file with a header line
#
# Data:
#
#      date,symbol,closing_price
#      6/20/2014,AAPL,90.91    
#      6/20/2014,MSFT,41.68    
#      6/20/2014,FB,64.5
#      6/19/2014,AAPL,91.86    
#      6/19/2014,MSFT,41.51    
#      6/19/2014,FB,64.34      

# (1) Easy way: call "reader.next()" to read over the first line
# (2) Or: store data in a dictionary

with open('inp-TAB+head.txt', 'r') as f:

    # Creata a CSV "DictReader" instance
    # and parse the header line
    reader = csv.DictReader(f, delimiter=',')

    # Read each row (looks like "reader" now returns a dict
    for row in reader:
        date = row["date"]
        symbol = row["symbol"]
        closing_price = float(row["closing_price"])
        print(date, symbol, closing_price)

print()

######################################################
# Read CSV file without header into a "dict"
#
#     you can still use DictReader by passing it the keys
#     as a fieldnames parameter.

with open('inp-TAB.txt', 'r') as f:

    # Creata a CSV "DictReader" instance
    # and parse the header line
    reader = csv.DictReader(f, delimiter='\t',
			fieldnames=["date", "symbol", "closing_price"])

    # Read each row (looks like "reader" now returns a dict
    for row in reader:
        date = row["date"]
        symbol = row["symbol"]
        closing_price = float(row["closing_price"])
        print(date, symbol, closing_price)

print()

######################################################
# Writing a CSV file 
#

today_prices = { 'AAPL' : 90.91, 'MSFT' : 41.68, 'FB' : 64.5 }

with open('out1.txt','w') as f:

    # Create a CSV writer with delimiter ","
    writer = csv.writer(f, delimiter=',')

    # Unpack a dict item into stock, price
    for stock, price in today_prices.items():
        writer.writerow([stock, price])
#                       ^^^^^^^^^^^^^^   write it out as a LIST !!!


#############################################################
# If data is already is LIST form

results = [["test1", "success", "Monday"],
           ["test2", "success, kind of", "Tuesday"],
           ["test3", "failure, kind of", "Wednesday"],
           ["test4", "failure, utter", "Thursday"]]
#                          ^^^^ Notice data has a COMMA !!!

with open('out2.txt','w') as f:
    
    # Create a CSV writer with delimiter ","
    writer = csv.writer(f, delimiter=',')
    
    # Unpack a dict item into stock, price
    for x in results:
        print(x)
        writer.writerow(x)

