53 lines
1.6 KiB
Markdown
53 lines
1.6 KiB
Markdown
---
|
|
id: practical-python-6.6
|
|
source_exercise_id: "6.6"
|
|
title: "Using a generator to produce data"
|
|
section: "6.2 Customizing Iteration"
|
|
source_path: "06_Generators/02_Customizing_iteration.md"
|
|
source_repo: "https://github.com/dabeaz-course/practical-python"
|
|
source_commit: "93dca856b41c61a0a0f85ae334116e4c125629ea"
|
|
student_visible_solution: false
|
|
has_private_solution: false
|
|
skip: false
|
|
---
|
|
|
|
# Exercise 6.6: Using a generator to produce data
|
|
|
|
> Source: Practical Python Programming, `06_Generators/02_Customizing_iteration.md`.
|
|
|
|
### Exercise 6.6: Using a generator to produce data
|
|
|
|
If you look at the code in Exercise 6.5, the first part of the code is producing
|
|
lines of data whereas the statements at the end of the `while` loop are consuming
|
|
the data. A major feature of generator functions is that you can move all
|
|
of the data production code into a reusable function.
|
|
|
|
Modify the code in Exercise 6.5 so that the file-reading is performed by
|
|
a generator function `follow(filename)`. Make it so the following code
|
|
works:
|
|
|
|
```python
|
|
>>> for line in follow('Data/stocklog.csv'):
|
|
print(line, end='')
|
|
|
|
... Should see lines of output produced here ...
|
|
```
|
|
|
|
Modify the stock ticker code so that it looks like this:
|
|
|
|
|
|
```python
|
|
if __name__ == '__main__':
|
|
for line in follow('Data/stocklog.csv'):
|
|
fields = line.split(',')
|
|
name = fields[0].strip('"')
|
|
price = float(fields[1])
|
|
change = float(fields[4])
|
|
if change < 0:
|
|
print(f'{name:>10s} {price:>10.2f} {change:>10.2f}')
|
|
```
|
|
|
|
## 关联来源
|
|
|
|
- [[summaries/02_Customizing_iteration]]
|