Repository navigation
Expand file tree
/
Copy pathwork6.py
More file actions
183 lines (130 loc) · 3.47 KB
/
Copy pathwork6.py
File metadata and controls
183 lines (130 loc) · 3.47 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
f = open('first.txt', 'w')
f.write('Hello\n')
f.write('World\n')
f.writelines(['Hello\n', 'Python\n'])
f = open('first.txt', 'r')
# print(f.read())
# for line in f:
# print(line.replace('\n', ''))
print(f.readlines())
f.close()
with open('first.txt', 'r') as f:
for line in f:
print(line.replace('\n', ''))
print('end')
##### байты
s = 'Hello world'
sb = b'Hello bytes'
print(s, type(s))
print(sb, type(sb))
print(s[1])
print(sb[1])
print(s[1:3])
print(sb[1:3])
for item in sb:
print(item)
sb = b'Ad'
# по ascii таблице A = 65, d = 100
print(sb[0], sb[1])
s = 'Hello world'
sb = s.encode('ascii')
print(sb, type(sb))
s = 'Hello world Мир'
sb = s.encode('utf-8')
print(sb, type(sb))
s = sb.decode('utf-8')
print(s, type(s))
# файлы с байтами
with open('bytes.txt', 'wb') as f:
f.write(b'Hello bytes')
with open('bytes.txt', 'r', encoding='ascii') as f:
print(f.read())
with open('bytes.txt', 'wb') as f:
str = 'Привет мир'
f.write(str.encode('utf-8'))
with open('bytes.txt', 'r', encoding='utf-8') as f:
print(f.read())
# чтение
with open('bytes.txt', 'wb') as f:
str = 'Привет мир'
f.write(str.encode('utf-8'))
# то же самое:
with open('bytes.txt', 'w', encoding='utf-8') as f:
f.write('Привет мир')
with open('bytes.txt', 'rb') as f:
result = f.read()
print(result)
print(type(result))
s = result.decode()
print(s)
# запись и чтение обЪекта в файл
person = {'name': 'Max', 'phones': [123, 345]}
# запись
with open('person.dat', 'wb') as f:
name = person['name']
f.write(f'{name}\n'.encode('utf-8'))
phones = person['phones']
for phone in phones:
f.write(f'{phone}\n'.encode('utf-8'))
print('объект записан')
# чтение
with open('person.dat', 'rb') as f:
result = f.readlines()
person = {}
person['name'] = result[0].decode('utf-8').replace('\n', '')
phones = []
for bphone in result[1:]:
phones.append(bphone.decode('utf-8').replace('\n', ''))
print(person)
# запись и чтение обЪекта в файл с помощью pickle
import pickle
person = {'name': 'Max', 'phones': [123, 345], 'age': 20}
with open('person.dat', 'wb') as f:
pickle.dump(person, f)
print('Объект записан')
with open('person.dat', 'rb') as f:
pickle.load(f)
print(person)
# работа с json
import json
friends = [
{'name': 'Max', 'age': 23, 'phones': [123, 234]},
{'name': 'Leo', 'age': 33},
['asdasdas', 214124],
'adadad',
1234,
True
]
print(friends)
print(type(friends))
json_friends = json.dumps(friends)
print(json_friends)
print(type(json_friends))
friends = json.loads(json_friends)
print(friends)
print(type(friends))
# работа с json и файлом
friends = [
{'name': 'Max', 'age': 23, 'phones': [123, 234]},
{'name': 'Leo', 'age': 33},
['asdasdas', 214124],
'adadad',
1234,
True
]
with open('friends.json', 'w') as f:
json_friends = json.dump(friends, f)
with open('friends.json', 'r') as f:
friends = json.load(f)
print(friends)
print(type(friends))
#
import json
favourite_tracks = [
{'name': 'Вечная любовь', 'artist': 'Агата Кристи'},
{'name': 'Angel', 'artist': 'Massive Attack'},
{'name': 'Jamming', 'artist': 'Bob Marley'}
]
with open('tracks.json', 'w', encoding='utf-8') as f:
json.dump(favourite_tracks, f)
print('Выполнено')