-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtxt.py
More file actions
92 lines (73 loc) · 2.07 KB
/
txt.py
File metadata and controls
92 lines (73 loc) · 2.07 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
##############################################################################
# The program shows how works reading and writing for a txt and csv file
##############################################################################
import random
import csv
def txt_read():
file = open("data/test.txt", "r")
line = file.readline().rstrip() # strip()
while line:
print(line)
# print(repr(line))
line = file.readline()
if line == "\n":
line = " "
continue
else:
line = line.rstrip()
file.close()
def txt_read_easier():
file = open("data/test.txt", "r")
text = file.read()
print(text)
file.close()
def txt_with():
with open("data/test.txt", "r") as file:
for line in file:
print(line.rstrip())
def txt_write():
file = open("data/test.txt", "w") # a = append
for i in range(10):
random_n = random.randint(1, 10000)
if i % 2 == 0:
space = "\t"
else:
space = ""
file.write("{}number, {} =>, {}\n".format(space, i, random_n))
file.close()
# Binary file is almost the same
# file= open("test.txt","rb"), we must add 'b' and we know what we want to read
def txt_read_del():
delimiter = ","
tab = []
with open("data/movies.csv", "r") as file:
for line in file:
line = line.rstrip()
tab.append(line.split(delimiter))
print(tab)
def csv_read():
tab = []
with open("data/movies.csv", "r") as file:
r = csv.reader(file)
for line in r:
print(line)
# def csv_read():
# tab = []
# with open("data.csv","r") as file:
# r=csv.DictReader(file)
# for line in r:
# print("{:.<60} {}".format(line["Title"],line["Rating"]))
def csv_dict():
tab = []
with open("data/movies.csv", "r") as file:
r = csv.DictReader(file)
for line in r:
tab.append(line)
print(line)
txt_read()
# txt_read_easier()
# txt_with()
# txt_write()
# txt_read_del()
# csv_read()
# csv_dict()