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

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问