python - 读入数据
问题描述
我正在学习如何将数据读入 Python,我被困在作业中的以下几项上,以使我当前的代码在下面工作。
函数 read_dow 有 2 个参数“file_path”,它包含一个数据集路径的字符串和“num_lines”,它是一个整数。
- 从“file_path”读取每一行。
- 用空字符串 ("") 替换换行符 "\n" 的所有实例。提示:使用字符串的替换功能
- 使用逗号作为分隔符分隔列提示:使用字符串的拆分方法
将不带“\n”字符的分隔数据行追加到列表中。
读完“num_lines”行后,停止读入新行。
返回一个嵌套列表,其中列表中的每个元素都是一行数据。
_
def read_dow(file_path, num_lines=5):
'''
Reads in num lines of data from file_path.
Parameters
----------
file_path: string containing file path to a dataset
num_lines: integer containing the number of lines to read
Returns
-------
Nested List of data read in from the file_path
'''
with open(file_path, 'r') as fin:
for line in fin:
line.replace('\n','')
line.split(",")
print(line)
new_list = []
for num_lines in fin:
new_list.append(num_lines)
这是正在运行以测试答案的代码。
# Reading in 3 lines of data and testing your answer
ans_3lines = read_dow('data/dow_jones_index.data', num_lines=3)
sol_3lines = [
['quarter', 'stock', 'date', 'open', 'high', 'low', 'close', 'volume',
'percent_change_price', 'percent_change_volume_over_last_wk',
'previous_weeks_volume',
'next_weeks_open', 'next_weeks_close',
'percent_change_next_weeks_price', 'days_to_next_dividend',
'percent_return_next_dividend'],
['1', 'AA', '1/7/2011', '15.82', '16.72', '15.78', '16.42',
'239655616', '3.79267', '', '', '16.71',
'15.97', '-4.42849', '26', '0.182704'],
['1', 'AA', '1/14/2011', '16.71', '16.71', '15.64', '15.97',
'242963398', '-4.42849', '1.380223028',
'239655616', '16.19', '15.79', '-2.47066', '19', '0.187852']
]
assert_equal(ans_3lines, sol_3lines, msg='Your answer does not match
the solutions')
# Reading in 5 lines of data and testing your answer
ans_5lines = read_dow('data/dow_jones_index.data')
sol_5lines = [
['quarter', 'stock', 'date', 'open', 'high', 'low', 'close', 'volume',
'percent_change_price', 'percent_change_volume_over_last_wk',
'previous_weeks_volume',
'next_weeks_open', 'next_weeks_close',
'percent_change_next_weeks_price', 'days_to_next_dividend',
'percent_return_next_dividend'],
['1', 'AA', '1/7/2011', '15.82', '16.72', '15.78', '16.42',
'239655616', '3.79267', '', '', '16.71',
'15.97', '-4.42849', '26', '0.182704'],
['1', 'AA', '1/14/2011', '16.71', '16.71', '15.64', '15.97',
'242963398', '-4.42849', '1.380223028',
'239655616', '16.19', '15.79', '-2.47066', '19', '0.187852'],
['1', 'AA', '1/21/2011', '16.19', '16.38', '15.60', '15.79',
'138428495', '-2.47066', '-43.02495926',
'242963398', '15.87', '16.13', '1.63831', '12', '0.189994'],
['1', 'AA', '1/28/2011', '15.87', '16.63', '15.82', '16.13',
'151379173', '1.63831', '9.355500109',
'138428495', '16.18', '17.14', '5.93325', '5', '0.185989']
]
assert_equal(ans_5lines, sol_5lines, msg='Your answer does not match
the solutions')
解决方案
你不需要你在循环中做的任何事情。我的建议是删除它们并从一个干净的循环开始(你可以在里面保留一个打印)。
现在,为了只打印您想要的行,您实际上append
只需要将您需要的行打印到列表中。
lines_you_want = [] # This is how you create a list
lines_you_want.append(line) # This is how you append a line into the list
我会把剩下的留给你:)
推荐阅读
- virtualization - 由于系统要求,HAXM 未安装
- flutter - 具有剪辑/编辑功能的 Flutter Audioplayers,如 Just_ Audio player
- snowflake-cloud-data-platform - 替换雪花中的子查询
- python - 在一个类中,调用 x 函数从 y 函数返回一个列表值。Python
- postgresql - Postgresql:基于变量类型构建查询
- c# - [C#DataGridView]如何让datagridview在单元格编辑后接收到keydown事件,而不是单元格控件
- reactjs - 如何使用 react-chartjs-2 创建水平条形图?
- php - 我可以在同一个应用程序中同时使用适用于 PHP 和 NodeJs 的 Azure Web 应用服务吗?
- react-final-form - 如何在 react-final-form 中注销字段?
- java - java - 如何在Java项目中大摇大摆地处理标题字段?