-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbasics (4).py
More file actions
59 lines (34 loc) · 1.12 KB
/
Copy pathbasics (4).py
File metadata and controls
59 lines (34 loc) · 1.12 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import csv
import os
path = r"C:\Users\Vidyashree M C\Desktop\sample.csv"
# reader()
with open(path) as file:
obj = csv.reader(file) # data in list
print(obj)
for data in obj:
print(data)
#DictReader()
with open(path) as file:
obj = csv.DictReader(file)
for data in obj:
print(data)
# writing into csv
os.chdir(r"C:\Users\Vidyashree M C\PycharmProjects\Alpha4\files\csv_files")
# writer()
with open("example.csv", "w") as file:
obj = csv.writer(file) # accepts data in the form list
# writing single row
obj.writerow(["Employee_name", "E_ID"])
obj.writerow(["John", 10])
# writing multiple rows
data = ["sita", 3], ["ram", 1]
obj.writerows(data)
# DictWriter()
with open("data.csv", "w") as file:
obj = csv.DictWriter(file, ["E_name", "E_ID"])
# writing header to the file
obj.writeheader()
# writing single row
obj.writerow({"E_name": "John", "E_ID": 8})
# writing multiple rows
obj.writerows([{"E_name": "John", "E_ID": 8}, {"E_name": "John", "E_ID": 8}])