Add Python notes

This commit is contained in:
2022-05-06 00:49:26 +03:00
parent 4c0cdb2f71
commit 1863176b39
57 changed files with 1205 additions and 5 deletions

View File

@@ -0,0 +1,3 @@
spam
egg
Italia

BIN
python/файлы/obj.pkl Executable file

Binary file not shown.

2
python/файлы/objects.txt Executable file
View File

@@ -0,0 +1,2 @@
41,42,43
[1, 5, 10]&{'food': 'spam', 'tool': 'wrench'}

13
python/файлы/пикл.py Executable file
View File

@@ -0,0 +1,13 @@
#Файл нельзя называть pickle
import pickle
#Сохранение объектов в файлах при помощи pickle
F = open("obj.pkl", "wb")
D = {'meal': 'burger', 'genius': 'heiakim'}
pickle.dump(D, F)
F.close()
#Чтение объектов
F = open('obj.pkl', 'rb')
print(pickle.load(F))

84
python/файлы/файлы.py Executable file
View File

@@ -0,0 +1,84 @@
#создание файлов. w - write
text_file = open('data_file.txt', 'w')
#Запись в файл
text_file.write('Hello\n')
text_file.write('World')
#Закрытие файла
text_file.close()
#По умолчанию файлы открываются в режиме r - read
text_file = open('data_file.txt')
#Чтение из файла. (в скобках можно указать кол-во символов для чтения)
print(text_file.read())
print('\n')
#Чтение одной строки. (в скобках можно указать кол-во символов для чтения)
text_file = open('data_file.txt')
S = text_file.readline()
S = text_file.readline()
print(S)
print('\n')
#Чтение файла в список строк
text_file = open('data_file.txt')
L = text_file.readlines()
print(L)
print('\n')
#Запись строк из списка в файл
text_file = open('data_file.txt', 'w')
tmp_list = ['spam\n', 'egg\n', 'Italia']
text_file.writelines(tmp_list)
text_file = open('data_file.txt')
print(text_file.read())
print('\n****************')
#Итератор файла
text_file = open('data_file.txt')
for S in text_file:
print(S * 2)
print('****************\n')
#Изменяет текущую позицию в файле для следующей операции, смещая ее на N байтов от начала файла.
text_file = open('data_file.txt')
text_file.seek(7)
print(text_file.read())
print('\n')
#Запись объектов
objects = open('objects.txt', 'w')
L = [1, 5, 10]
X, Y, Z = 41, 42, 43
D = {'food': 'spam', 'tool': 'wrench'}
objects.write('{0},{1},{2}\n'.format(X, Y, Z))
objects.write(str(L) + '&' + str(D) + '\n')
objects = open('objects.txt')
print(objects.read())
#Чтение объектов
objects = open('objects.txt')
line = objects.readline()
#Удаление символа конца строки
line = line.rstrip()
print(line)
numbers = line.split(',')
L = [int(x) for x in numbers]
print(L)
line = objects.readline()
line = line.rstrip()
elements = line.split('&')
result = [eval(x) for x in elements]
print(result)