-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path08thFileHandlinginPython.py
More file actions
73 lines (37 loc) · 1.89 KB
/
08thFileHandlinginPython.py
File metadata and controls
73 lines (37 loc) · 1.89 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# ! File Handling
import csv
with open(r'Files\biopic.txt') as f: # ! default mode of open is r (read mode)
print(f.read())
# ! These Functions below work only in r,r+, w+ and a+
# * read() returns all the things contained in the text file
# * readline() reads one line with \n included
# * readlines() returns a list which contain each line as an element (with \n included at each line)
with open(r'Files\biopic.txt','w') as f: # ! w mode will create a new file if doesn't exist or overwrites(clears the file and starts from empty file)
f.write('I am writing something new\n')
f.write('If I don\'t add \\n this will be printed continuesly')
f.write('Like so')
import csv
with open(r'Files\t.csv','r',newline='\r\n') as d: # ! newline='\r\n' removes any empty lines present in the csv file for a more presentable output
a = csv.reader(d) # * This produces a readable object (which means loop through it to get the values)
print(a) # * This prints the object representation
for i in a:
print(i) # * This prints each value (row) in the csv file (except the empty rows as specified by newline='\r\n')
with open(r'Files\t.csv','a',newline='') as d: # ! We just don't add any empty lines when adding new data by using newline = ''
a=csv.writer(d) # * We created a writer object which will help us
a.writerow([1,2,3])
a.writerow([10,11,12])
print('After Adding new things to the csv file:')
with open(r'Files\t.csv','r',newline='\r\n') as d: # ! look at line 404
a = csv.reader(d)
head = next(a) # * Skips the header row
for i in a:
print(i)
#TODO learn about Dictreader and Dictwriter functions for csv
with open(r'Files\t.csv','a',newline='') as d:
a = {1:1,2:2,3:3}
b = csv.DictWriter(d)
b.writerow(a)
with open(r'Files\t.csv','r',newline='\r\n') as d:
a = csv.DictReader(d)
for i in a:
print(i)