51 lines
1.0 KiB
Python
51 lines
1.0 KiB
Python
#!/usr/bin/env python
|
|
|
|
import os,sys
|
|
import csv
|
|
|
|
# Q1
|
|
exampleFile = open('example.csv')
|
|
rd = csv.reader(exampleFile)
|
|
|
|
print(rd)
|
|
|
|
data = list(rd)
|
|
print(data[3-1][4-1])
|
|
|
|
# Q4
|
|
exampleFile = open('example.csv')
|
|
rd = csv.reader(exampleFile)
|
|
|
|
for row in rd:
|
|
print('Row #' + str(rd.line_num) + ' ' + str(row))
|
|
|
|
# Q5
|
|
import csv
|
|
|
|
outputFile = open('new-example.csv', 'w', newline='')
|
|
outputWriter = csv.writer(outputFile)
|
|
outputWriter.writerow(['4/11/2014 12:44', 'watermelon', 100, 7.8])
|
|
outputFile.close()
|
|
|
|
# Q6
|
|
import csv
|
|
|
|
exampleFile = open('example.csv')
|
|
rd = csv.DictReader(exampleFile, ['date', 'product', 'quantity', 'price'])
|
|
|
|
# for row in rd:
|
|
# print(row['time'], row['name'], row['amount'])
|
|
|
|
# Q7
|
|
import csv
|
|
|
|
outputFile = open('new-example2.csv', 'w', newline='')
|
|
outputDictWriter = csv.DictWriter(outputFile, ['date', 'product', 'quantity', 'price'])
|
|
outputDictWriter.writeheader()
|
|
outputDictWriter.writerow({'date': 'Alice', 'product': 'grape', 'quantity': 20, 'price': 99.9})
|
|
outputFile.close()
|
|
|
|
|
|
|
|
|
|
print('helloworld') |