qq_43296461 2018-09-27 11:07
浏览 342

python read in data请大神修改代码

Reading In Data
In the code cell below the function read_dow has 3 parameters "file_path" which contains a string to the path of a dataset, columns which contains a list of the indices of columns to select, and "num_lines" which is an integer.

For this problem use Python's built-in functions to read in data from "file_path".

Read in each row from "file_path".
replace all instances of the new line character ("\n") with an empty string ("").
Hint: Use the replace function of a string
seperate the columns using a comma as a delimiter
Hint: Use the split function of a string
only select the indices inside of a row and store them in a seperate list,
append the seperated row of data without the "\n" character and only the columns indices passed in to a list.
if columns is None select all rows.
stop reading in new rows once you have read in "num_lines" lines.
Return a nested list where each element in the list is a row of data.

def read_dow(filePath, columns=None, num_lines=3):
smallFile = []
bigFile = []
with open(filePath, 'r') as fin:
iCounter = 0
list=[]
for list in fin:
for i in list:
iCounter += 1
if columns != None and iCounter > 4 :
break
else:
temprow = i.replace("\n", "").split(",")
smallFile.append(temprow)
bigFile.append(smallFile)
return bigFile

  • 写回答

0条回答

    报告相同问题?

    悬赏问题

    • ¥15 如何在scanpy上做差异基因和通路富集?
    • ¥20 关于#硬件工程#的问题,请各位专家解答!
    • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
    • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
    • ¥30 截图中的mathematics程序转换成matlab
    • ¥15 动力学代码报错,维度不匹配
    • ¥15 Power query添加列问题
    • ¥50 Kubernetes&Fission&Eleasticsearch
    • ¥15 報錯:Person is not mapped,如何解決?
    • ¥15 c++头文件不能识别CDialog