Skip to content

Commit 5be023f

Browse files
committed
Refactoring. Using black code style
1 parent bd6e746 commit 5be023f

7 files changed

+32
-26
lines changed

csv__examples/reader_csv.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33

4-
__author__ = 'ipetrash'
4+
__author__ = "ipetrash"
55

66

77
import csv
88

9-
with open('input.csv') as f:
9+
10+
with open("input.csv") as f:
1011
csv_reader = csv.reader(f, delimiter=";")
1112
for row in csv_reader:
1213
print(row)
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33

4-
__author__ = 'ipetrash'
4+
__author__ = "ipetrash"
55

66

77
import csv
88

9-
with open('input.csv') as f:
9+
10+
with open("input.csv") as f:
1011
csv_reader = csv.DictReader(f, delimiter=";")
1112
for row in csv_reader:
12-
print(row['name'], row['address'])
13+
print(row["name"], row["address"])

csv__examples/reader_csv__auto_dialect__using_csv_Sniffer.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33

4-
__author__ = 'ipetrash'
4+
__author__ = "ipetrash"
55

66

77
import csv
88

9-
with open('input.csv') as f:
9+
10+
with open("input.csv") as f:
1011
dialect = csv.Sniffer().sniff(f.readline())
1112
# # With delimiters:
12-
# dialect = csv.Sniffer().sniff(f.readline(), delimiters=[',', ';'])
13+
# dialect = csv.Sniffer().sniff(f.readline(), delimiters=[",", ";"])
1314
f.seek(0)
1415

1516
csv_reader = csv.reader(f, dialect=dialect)

csv__examples/reader_csv__from_text.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33

4-
__author__ = 'ipetrash'
4+
__author__ = "ipetrash"
55

66

77
import csv
8+
from io import StringIO
9+
810

911
text = """\
1012
id;name;address;zip
@@ -20,11 +22,8 @@
2022
print()
2123

2224
# Variant 2
23-
from io import StringIO
24-
2525
csv_reader = csv.reader(StringIO(text), delimiter=";")
2626
for row in csv_reader:
2727
print(row)
2828

2929
print()
30-

csv__examples/writer_csv.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33

4-
__author__ = 'ipetrash'
4+
__author__ = "ipetrash"
55

66

77
# SOURCE: https://docs.python.org/3/library/csv.html#csv.writer
88

99

1010
import csv
11-
with open('eggs.csv', 'w', newline='') as csvfile:
11+
12+
13+
with open("eggs.csv", "w", newline="") as csvfile:
1214
writer = csv.writer(csvfile)
13-
writer.writerow(['Spam'] * 5 + ['Baked Beans'])
14-
writer.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])
15+
writer.writerow(["Spam"] * 5 + ["Baked Beans"])
16+
writer.writerow(["Spam", "Lovely Spam", "Wonderful Spam"])
+7-6
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33

4-
__author__ = 'ipetrash'
4+
__author__ = "ipetrash"
55

66

77
# SOURCE: https://docs.python.org/3/library/csv.html#csv.DictWriter
88

99

1010
import csv
1111

12-
with open('names.csv', 'w', encoding='utf-8', newline='') as csvfile:
13-
fieldnames = ['first_name', 'last_name']
12+
13+
with open("names.csv", "w", encoding="utf-8", newline="") as csvfile:
14+
fieldnames = ["first_name", "last_name"]
1415

1516
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
1617
writer.writeheader()
1718

18-
writer.writerow({'first_name': 'Baked', 'last_name': 'Beans'})
19-
writer.writerow({'first_name': 'Lovely', 'last_name': 'Spam'})
20-
writer.writerow({'first_name': 'Wonderful', 'last_name': 'Spam'})
19+
writer.writerow({"first_name": "Baked", "last_name": "Beans"})
20+
writer.writerow({"first_name": "Lovely", "last_name": "Spam"})
21+
writer.writerow({"first_name": "Wonderful", "last_name": "Spam"})

csv__examples/writer_list_of_dict.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33

4-
__author__ = 'ipetrash'
4+
__author__ = "ipetrash"
55

66

77
# SOURCE: https://docs.python.org/3/library/csv.html#csv.DictWriter
@@ -10,13 +10,14 @@
1010

1111
import csv
1212

13+
1314
items = [
14-
{'name': 'bob', 'age': 25, 'weight': 200},
15-
{'name': 'jim', 'age': 31, 'weight': 180}
15+
{"name": "bob", "age": 25, "weight": 200},
16+
{"name": "jim", "age": 31, "weight": 180},
1617
]
1718

1819
keys = items[0].keys()
19-
with open('people.csv', 'w', encoding='utf-8', newline='') as f:
20+
with open("people.csv", "w", encoding="utf-8", newline="") as f:
2021
dict_writer = csv.DictWriter(f, keys)
2122
dict_writer.writeheader()
2223
dict_writer.writerows(items)

0 commit comments

Comments
 (0)