-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist_csv_operation.py
More file actions
42 lines (40 loc) · 1.15 KB
/
list_csv_operation.py
File metadata and controls
42 lines (40 loc) · 1.15 KB
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
'''
@Author : Md. Shamimul Islam
@Written : 10/02/2019
@Description: python List and CSV operation
'''
print("--------------Make a list of list from a csv file---------------")
import csv
fp=open('Advertising.csv',"Ur")
data_list=[]
for line in fp:
data_list.append(line.strip().split(','))
#strip = remove space in the first and end of the tuple
#stlit=seperated by comma after every tuple's element
fp.close
print(data_list)
for line in data_list:
print(','.join(line))
"""
#function
def load_file(filename):
fp = open(filename, 'Ur')
data_list = []
for line in fp:
data_list.append(line.strip().split(','))
fp.close()
return data_list
#advanced python
def load_file(filename):
with open(filename, 'Ur') as fp:
data_list = [tuple(line.strip().split(",") for line in fp]
"""
print("--------------Write a CVS file from a tuple datasets---------------")
def save_file(filename,data_list):
fp=open(filename,'w')
for line in data_list:
fp.write(','.join(line) + '\n')
fp.close
save_file('b.csv',data_list)
data_list=(('2','4','3'),('6','0','8'))
save_file('b_1.csv',data_list)