forked from sureshdsk/py-codes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist_to_csv.py
46 lines (34 loc) · 1.12 KB
/
list_to_csv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import csv
import os
def ReadCSVasList(csv_file):
try:
with open(csv_file) as csvfile:
reader = csv.reader(csvfile, dialect='excel', quoting=csv.QUOTE_NONNUMERIC)
datalist = []
datalist = list(reader)
return datalist
except IOError as (errno, strerror):
print("I/O error({0}): {1}".format(errno, strerror))
return
def WriteListToCSV(csv_file,csv_columns,data_list):
try:
with open(csv_file, 'w') as csvfile:
writer = csv.writer(csvfile, dialect='excel', quoting=csv.QUOTE_NONNUMERIC)
writer.writerow(csv_columns)
for data in data_list:
writer.writerow(data)
except IOError as (errno, strerror):
print("I/O error({0}): {1}".format(errno, strerror))
return
csv_columns = ['Row','Name','Country']
currentPath = os.getcwd()
csv_file = currentPath + "/csv/Names.csv"
csv_data_list = ReadCSVasList(csv_file)
print csv_data_list
# To Ignore 1st Row (Headers)
csv_data_list.pop(0)
print csv_data_list
# append to list
csv_data_list.append(['6', 'Suresh', 'India'])
print csv_data_list
WriteListToCSV(csv_file,csv_columns,csv_data_list)