8000 Added section 7 and reorganised section 6. · Saigelaw/complete-python-course@a628745 · GitHub
[go: up one dir, main page]

Skip to content

Commit a628745

Browse files
committed
Added section 7 and reorganised section 6.
1 parent 80edac9 commit a628745

File tree

27 files changed

+412
-0
lines changed

27 files changed

+412
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
data.db
12
*.key
23
*.indd
34
*.png

section6/files_project/app.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
my_file = open('data.txt', 'r')
2+
file_content = my_file.read()
3+
4+
my_file.close()
5+
6+
print(file_content)
7+
8+
user_name = input('Enter your name: ')
9+
10+
my_file_writing = open('data.txt', 'w')
11+
my_file_writing.write(user_name)
12+
13+
my_file_writing.close()

section6/files_project/cars_json.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[{"make": "Ford", "model": "Fiesta"}, {"make": "Ford", "model": "Focus"}]

section6/files_project/csv_data.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
name,age,university,degree
2+
rolf,25,mit,computer science
3+
jose,90,oxford,computing
4+
anna,30,cambridge,physics

section6/files_project/csv_read.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
file = open('csv_data.txt', 'r')
2+
lines = file.readlines()
3+
file.close()
4+
5+
lines = [line.strip() for line in lines[1:]]
6+
7+
for line in lines:
8+
person_data = line.split(',')
9+
name = person_data[0].title()
10+
age = person_data[1]
11+
university = person_data[2].title()
12+
degree = person_data[3].capitalize()
13+
14+
print(f'{name} is {age}, studying {degree} at {university}.')

section6/files_project/data.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Rolf

section6/files_project/friends.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Ask the user for a list of 3 friends
2+
# For each friend, we'll tell the user whether they are nearby
3+
# For each nearby friend, we'll save their name to `nearby_friends.txt`
4+
5+
friends = input('Enter three friend names, separated by commas (no spaces, please): ').split(',')
6+
7+
people = open('people.txt', 'r')
8+
people_nearby = [line.strip() for line in people.readlines()]
9+
10+
people.close()
11+
12+
friends_set = set(friends)
13+
people_nearby_set = set(people_nearby)
14+
15+
friends_nearby_set = friends_set.intersection(people_nearby_set)
16+
17+
nearby_friends_file = open('nearby_friends.txt', 'w')
18+
19+
for friend in friends_nearby_set:
20+
print(f'{friend} is nearby! Meet up with them.')
21+
nearby_friends_file.write(f'{friend}\n')
22+
23+
nearby_friends_file.close()
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"friends": [
3+
{
4+
"name": "Jose",
5+
"degree": "Applied Computing"
6+
},
7+
{
8+
"name": "Rolf",
9+
"degree": "Computer Science"
10+
},
11+
{
12+
"name": "Anna",
13+
"degree": "Physics"
14+
}
15+
]
16+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import json
2+
3+
with open('friends_json.txt', 'r') as file:
4+
file_contents = json.load(file) # reads file and turns it to dictionary
5+
6+
print(file_contents['friends'][0])
7+
8+
9+
cars = [
10+
{'make': 'Ford', 'model': 'Fiesta'},
11+
{'make': 'Ford', 'model': 'Focus'}
12+
]
13+
14+
with open('cars_json.txt', 'w') as file:
15+
json.dump(cars, file)
16+
17+
18+
my_json_string = '[{"name": "Alfa Romeo", "released": 1950}]'
19+
20+
incorrect_car = json.loads(my_json_string)
21+
print(incorrect_car[0]['name'])
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import json
2+
3+
file = open('friends_json.txt', 'r')
4+
file_contents = json.load(file) # reads file and turns it to dictionary
5+
6+
file.close()
7+
8+
print(file_contents['friends'][0])
9+
10+
cars = [
11+
{'make': 'Ford', 'model': 'Fiesta'},
12+
{'make': 'Ford', 'model': 'Focus'}
13+
]
14+
15+
file = open('cars_json.txt', 'w')
16+
json.dump(cars, file)
17+
file.close()
18+
19+
my_json_string = '[{"name": "Alfa Romeo", "released": 1950}]'
20+
21+
incorrect_car = json.loads(my_json_string)
22+
print(incorrect_car[0]['name'])

0 commit comments

Comments
 (0)
0