Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Домашняя работа для второго урока #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions hw_Lesson_02/1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import csv

print("Чтение данных из csv:")
with open('1_in.csv') as f_n:
f_n_reader = csv.reader(f_n)
for row in f_n_reader:
print(row)

print("Запись данных в файл 1_out.csv")

data = [['hostname', 'vendor', 'model', 'location'],
['kp1', 'Cisco', '2960', 'Moscow, str'],
['kp2', 'Cisco', '2960', 'Novosibirsk, str'],
['kp3', 'Cisco', '2960', 'Kazan, str'],
['kp4', 'Cisco', '2960', 'Tomsk, str']]

with open('1_out.csv', 'w', newline='') as f_n:
f_n_writer = csv.writer(f_n)
for row in data:
f_n_writer.writerow(row)

print("Проверка записанных данных:")
with open('1_out.csv') as f_n:
print(f_n.read())

5 changes: 5 additions & 0 deletions hw_Lesson_02/1_in.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
hostname,vendor,model,location
kp1,Cisco,2960,Moscow
kp2,Cisco,2960,Novosibirsk
kp3,Cisco,2960,Kazan
kp4,Cisco,2960,Tomsk
25 changes: 25 additions & 0 deletions hw_Lesson_02/2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import json

print("чтение данных из json")
with open('2_in.json') as f_n:
objs = json.load(f_n)

for section, commands in objs.items():
print(section, ":", commands)

print("запись данных в файл 2_out.json")

dict_to_json = {
"action": "msg",
"to": "account_name",
"from": "account_name",
"encoding": "ascii",
"message": "message"
}

with open('2_out.json', 'w') as f_n:
f_n.write(json.dumps(dict_to_json))

print("проверка записанных данных:")
with open('2_out.json') as f_n:
print(f_n.read())
8 changes: 8 additions & 0 deletions hw_Lesson_02/2_in.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"action": "msg",
"time": 123,
"to": "account_name",
"from": "account_name",
"encoding": "ascii",
"message": "message"
}
20 changes: 20 additions & 0 deletions hw_Lesson_02/3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import yaml

print("чтение данных из yaml")
with open('3_in.yaml') as f_n:
f_n_content = yaml.load(f_n)
print(f_n_content)

print("записываем данные в файл 3_out.yaml")
action_list = ['msg_1', 'msg_2', 'msg_3']

to_list = ['account_1', 'account_2', 'account_3']

data_to_yaml = {'action' : action_list, 'to' : to_list}

with open('3_out.yaml', 'w') as f_n:
yaml.dump(data_to_yaml, f_n)

print("проверка записанных данных:")
with open('3_out.yaml') as f_n:
print(f_n.read())
4 changes: 4 additions & 0 deletions hw_Lesson_02/3_in.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- action: msg_1
to: account_1
- action: msg_2
to: account_2
20 changes: 20 additions & 0 deletions hw_Lesson_02/4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import csv
import json

template = '{{"ФИО": "{}", "e-mail": "{}", "ЕГЭ": {{"ИКТ": {}, "общий": {}}}}}'
result = []
print("считывание данных из 4_in.csv:")
with open('4_in.csv') as fin:
reader = csv.reader(fin)
for line in reader:
print(line)
line = [x if x != '-' else 'null' for x in line]
result.append(json.loads(template.format(*(' '.join(line[0:3]), *line[3:]))))

print("запись данных в файл 4_out.json")
with open('4_out.json', 'w') as fh:
fh.write(json.dumps(result, sort_keys=True, ensure_ascii=False, indent=4))

print("проверка записанных данных:")
with open('4_out.json') as f_n:
print(f_n.read())
5 changes: 5 additions & 0 deletions hw_Lesson_02/4_in.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"������","����","��������","[email protected]","-","210"
"�������","����","����������","[email protected]","65","215"
"�������","����","���������","[email protected]","70","220"
"��������","�����","�������������","[email protected]","75","225"
"�������","���������","�����������","[email protected]","80","230"
30 changes: 30 additions & 0 deletions hw_Lesson_02/5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import csv
import yaml

items = []


def convert_to_yaml(line):
item = {
'id': int(line[0]),
'title_english': line[1],
'title_russian': line[2]
}
items.append(item)


print("считываем данные из файла 5_in.csv")
with open("5_in.csv", "r") as in_file:
reader = csv.reader(in_file)
next(reader) # skip headers
for line in reader:
print(line)
convert_to_yaml(line)

print("записываем данные в файл 5_out.yaml")
with open("5_out.yaml", "w", encoding="utf-8") as out_file:
out_file.write(yaml.dump(items, default_flow_style=False, allow_unicode=True))

print("проверка записанных данных:")
with open('5_out.yaml', encoding="utf-8") as f_n:
print(f_n.read())
3 changes: 3 additions & 0 deletions hw_Lesson_02/5_in.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
id, title_english, title_russian
1, A Title in English, �������� �� �������
2, Another Title, ������ ��������
16 changes: 16 additions & 0 deletions hw_Lesson_02/6.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import json
import yaml

print("чтение данных из json:")
with open('6_in.json') as f_n:
objs = json.load(f_n)

print(objs)

print("записываем данные в файл 6_out.yaml")
with open("6_out.yaml", "w", encoding="utf-8") as out_file:
out_file.write(yaml.dump(objs, default_flow_style=False, allow_unicode=True))

print("проверка записанных данных:")
with open('6_out.yaml', encoding="utf-8") as f_n:
print(f_n.read())
42 changes: 42 additions & 0 deletions hw_Lesson_02/6_in.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
[
{
"e-mail": "[email protected]",
"���": {
"���": null,
"�����": 210
},
"���": "������ ���� ��������"
},
{
"e-mail": "[email protected]",
"���": {
"���": 65,
"�����": 215
},
"���": "������� ���� ����������"
},
{
"e-mail": "[email protected]",
"���": {
"���": 70,
"�����": 220
},
"���": "������� ���� ���������"
},
{
"e-mail": "[email protected]",
"���": {
"���": 75,
"�����": 225
},
"���": "�������� ����� �������������"
},
{
"e-mail": "[email protected]",
"���": {
"���": 80,
"�����": 230
},
"���": "������� ��������� �����������"
}
]