首页 > 解决方案 > 读入数据

问题描述

我正在学习如何将数据读入 Python,我被困在作业中的以下几项上,以使我当前的代码在下面工作。

函数 read_dow 有 2 个参数“file_path”,它包含一个数据集路径的字符串和“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')

标签: python

解决方案


你不需要你在循环中做的任何事情。我的建议是删除它们并从一个干净的循环开始(你可以在里面保留一个打印)。

现在,为了只打印您想要的行,您实际上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

我会把剩下的留给你:)


推荐阅读