Files
coding-mentor-agent/kb/python-course-kb-practical-python/wiki/exercises/2-26-the-big-picture.md
T

1.9 KiB
Raw Blame History

id, source_exercise_id, title, section, source_path, source_repo, source_commit, student_visible_solution, has_private_solution, skip
id source_exercise_id title section source_path source_repo source_commit student_visible_solution has_private_solution skip
practical-python-2.26 2.26 The Big Picture 2.7 Objects 02_Working_with_data/07_Objects.md https://github.com/dabeaz-course/practical-python 93dca856b41c61a0a0f85ae334116e4c125629ea false false false

Exercise 2.26: The Big Picture

Source: Practical Python Programming, 02_Working_with_data/07_Objects.md.

Exercise 2.26: The Big Picture

Using the techniques in this exercise, you could write statements that easily convert fields from just about any column-oriented datafile into a Python dictionary.

Just to illustrate, suppose you read data from a different datafile like this:

>>> f = open('Data/dowstocks.csv')
>>> rows = csv.reader(f)
>>> headers = next(rows)
>>> row = next(rows)
>>> headers
['name', 'price', 'date', 'time', 'change', 'open', 'high', 'low', 'volume']
>>> row
['AA', '39.48', '6/11/2007', '9:36am', '-0.18', '39.67', '39.69', '39.45', '181800']
>>>

Let’s convert the fields using a similar trick:

>>> types = [str, float, str, str, float, float, float, float, int]
>>> converted = [func(val) for func, val in zip(types, row)]
>>> record = dict(zip(headers, converted))
>>> record
{'volume': 181800, 'name': 'AA', 'price': 39.48, 'high': 39.69,
'low': 39.45, 'time': '9:36am', 'date': '6/11/2007', 'open': 39.67,
'change': -0.18}
>>> record['name']
'AA'
>>> record['price']
39.48
>>>

Bonus: How would you modify this example to additionally parse the date entry into a tuple such as (6, 11, 2007)?

Spend some time to ponder what you’ve done in this exercise. We’ll revisit these ideas a little later.

Contents | Previous (2.6 List Comprehensions) | Next (3 Program Organization)

关联来源