Created
January 6, 2022 20:18
-
-
Save ploorp/a1780a821e79973860fb6c9a6bf050e5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6. What's With "with"? | |
with open('fun_file.txt') as close_this_file: | |
setup = close_this_file.readline() | |
punchline = close_this_file.readline() | |
print(setup) | |
7. What is a CSV File? | |
with open('logger.csv') as log_csv_file: | |
print(log_csv_file.read()) | |
8. Reading a CSV File | |
import csv | |
with open('cool_csv.csv') as cool_csv_file: | |
cool_csv_dict = csv.DictReader(cool_csv_file) | |
for i in cool_csv_dict: | |
print(i['Cool Fact']) | |
9. Reading different Types of CSV Files | |
import csv | |
with open('books.csv') as books_csv: | |
books_reader = csv.DictReader(books_csv, delimiter = '@') | |
isbn_list = [i['ISBN'] for i in books_reader] | |
10. Writing a CSV File | |
import csv | |
access_log = [{'time': '08:39:37', 'limit': 844404, 'address': '1.227.124.181'}, {'time': '13:13:35', 'limit': 543871, 'address': '198.51.139.193'}, {'time': '19:40:45', 'limit': 3021, 'address': '172.1.254.208'}, {'time': '18:57:16', 'limit': 67031769, 'address': '172.58.247.219'}, {'time': '21:17:13', 'limit': 9083, 'address': '124.144.20.113'}, {'time': '23:34:17', 'limit': 65913, 'address': '203.236.149.220'}, {'time': '13:58:05', 'limit': 1541474, 'address': '192.52.206.76'}, {'time': '10:52:00', 'limit': 11465607, 'address': '104.47.149.93'}, {'time': '14:56:12', 'limit': 109, 'address': '192.31.185.7'}, {'time': '18:56:35', 'limit': 6207, 'address': '2.228.164.197'}] | |
fields = ['time', 'address', 'limit'] | |
with open('logger.csv', 'w') as logger_csv: | |
log_writer = csv.DictWriter(logger_csv, fieldnames = fields) | |
log_writer.writeheader() | |
for i in access_log: | |
log_writer.writerow(i) | |
11. Reading a JSON File | |
import json | |
with open('message.json') as message_json: | |
message = json.load(message_json) | |
print(message['text']) | |
12. Writing a JSON File | |
import json | |
data_payload = [ | |
{'interesting message': 'What is JSON? A web application\'s little pile of secrets.', | |
'follow up': 'But enough talk!'} | |
] | |
with open('data.json', 'w') as data_json: | |
json.dump(data_payload, data_json) | |
13. Review | |
N/A |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment