Skip to content

Commit 1d46acd

Browse files
committed
refactoring
1 parent 28877c6 commit 1d46acd

File tree

4 files changed

+18
-20
lines changed

4 files changed

+18
-20
lines changed

demo-case/filter-some-row/use-python-csv-prototype_1/main.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def main ():
1919
writer = csv.writer(output_file)
2020

2121

22-
for row in reader: ## 一列一列巡迴
22+
for row in reader: ## 一列一列地巡迴
2323
# print(row)
2424

2525
if row[3] == '0': ## 排除第四個欄位是「0」的那一列(row)。第四個欄位指的是「欄位D」。

demo-case/filter-some-row/use-python-csv-prototype_2/main.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def main ():
1515
with open(input_csv) as input_file:
1616
reader = csv.reader(input_file)
1717

18-
for row in reader: ## 一列一列巡迴
18+
for row in reader: ## 一列一列地巡迴
1919
# print(row)
2020

2121
if row[3] == '0': ## 排除第四個欄位是「0」的那一列(row)。第四個欄位指的是「欄位D」。

demo-case/filter-some-row/use-python-csv/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ $ make
2121
## Reference
2222

2323
* https://docs.python.org/3/library/csv.html
24+
* https://docs.python.org/3/library/io.html
2425
* https://blog.gtwang.org/programming/python-csv-file-reading-and-writing-tutorial/

demo-case/filter-some-row/use-python-csv/main.py

+15-18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
#!/usr/bin/env python3
22

3+
# https://docs.python.org/3/library/csv.html
4+
# https://docs.python.org/3/library/io.html
5+
# https://blog.gtwang.org/programming/python-csv-file-reading-and-writing-tutorial/
6+
# https://en.wikipedia.org/wiki/Fluent_interface#PHP
37

48
import csv
59

@@ -12,13 +16,6 @@ def setFilePath (self, val):
1216
self._FilePath = val
1317
return self
1418

15-
_Handle = None
16-
def getHandle (self):
17-
return self._Handle
18-
def setHandle (self, val):
19-
self._Handle = val
20-
return self
21-
2219
_Data = []
2320
def getData (self):
2421
return self._Data
@@ -54,31 +51,31 @@ class AppFilter:
5451

5552
def run (self):
5653

57-
csv_source = FileCsvReader().setFilePath('var/input.csv').load()
54+
source_csv = FileCsvReader().setFilePath('var/input.csv').load()
5855

59-
data_source = csv_source.toArray()
56+
source_data = source_csv.toArray()
6057

61-
data_target = []
58+
target_data = []
6259

63-
for row_source, cells_source in enumerate(data_source):
60+
for source_row, source_cols in enumerate(source_data): ## 一列一列地巡迴
6461

65-
#if row_source == 0: # 不處理第一列
62+
#if source_row == 0: ## 不處理第一列
6663
# continue
6764

6865

69-
#print(cells_source);
66+
#print(source_cols);
7067

71-
if cells_source[3] == '0':
68+
if source_cols[3] == '0': ## 排除第四個欄位是「0」的那一列(row)。第四個欄位指的是「欄位D」。
7269
continue
7370

74-
data_target.append(cells_source);
71+
target_data.append(source_cols); ## 此列沒被排除,所以放到「target_data」。
7572

7673

77-
#print(data_target)
74+
#print(target_data)
7875

79-
csv_target = FileCsvWriter().setFilePath('var/output.csv').setData(data_target)
76+
target_csv = FileCsvWriter().setFilePath('var/output.csv').setData(target_data)
8077

81-
csv_target.save()
78+
target_csv.save() ## 把「target_data」寫到「var/output.csv」。
8279

8380
if __name__ == "__main__":
8481
AppFilter().run()

0 commit comments

Comments
 (0)