在python2.7.11中,为什么可以';我不能删除文件打开代码吗?

在python2.7.11中,为什么可以';我不能删除文件打开代码吗?,python,numpy,Python,Numpy,保存数据的.txt文件如下(来源:第2章中的“datingTestSet2.txt”): 代码: 此功能的结果是: datingDataMat datingLabels 40920 8.326976 0.953952 3 14488 7.153469 1.673904 2 26052 1.441871 0.805124 1 75136 13.147394

保存数据的
.txt
文件如下(来源:第2章中的“datingTestSet2.txt”):

代码:

此功能的结果是:

      datingDataMat                 datingLabels
40920   8.326976    0.953952           3
14488   7.153469    1.673904           2
26052   1.441871    0.805124           1
75136   13.147394   0.428964           1
38344   1.669788    0.134296           1
72993   10.141740   1.032955           1
35948   6.830792    1.213192           3
42666   13.276369   0.543880           3
67497   8.631577    0.749278           1
35483   12.273169   1.508053           3
50242   3.723498    0.831917           1
...     ...         ...               ...
我的问题是:

  • 当我刚刚删除Code2(
    fr=open(filename)
    ,它位于
    索引=0
    )之上时, 函数的结果变成全零矩阵和全零向量。 为什么我不能删除代码2?第一行(
    fr=open(filename)

  • 当我仅仅添加代码1(
    arr=fr.readlines()
    )时,它是错误的。为什么

    returnMat[index,:] = listFromLine[0:3]
    
    IndexError: index 0 is out of bounds for axis 0 with size 0
    
  • 1) 无法删除代码2行,因为该行:

    numberOfLines = len(fr.readlines())        #get the number of lines in the file
    
    在该行中,您将读取到文件的末尾。再次打开它会将您置于文件的开头

    2) 与上面的答案类似,如果调用readLines()读取所有行并将文件光标移动到文件末尾。。。因此,如果您再次尝试读取文件上的行,则没有任何内容可读取,因此将失败。

    1)您无法删除代码2行,因为该行:

    numberOfLines = len(fr.readlines())        #get the number of lines in the file
    
    在该行中,您将读取到文件的末尾。再次打开它会将您置于文件的开头


    2) 与上面的答案类似,如果调用readLines()读取所有行并将文件光标移动到文件末尾。。。因此,如果您再次尝试读取文件上的行,则没有任何内容可读取,因此将失败。

    您位于文件末尾。因此,第二次尝试读取文件内容不会产生任何结果。您需要返回到文件的开头。使用:

    fr.seek(0)
    
    而不是您的:

    fr = open(filename)  # Code2!!!!!!!!!!!!!!!!!!!!!
    

    您位于文件的末尾。因此,第二次尝试读取文件内容不会产生任何结果。您需要返回到文件的开头。使用:

    fr.seek(0)
    
    而不是您的:

    fr = open(filename)  # Code2!!!!!!!!!!!!!!!!!!!!!
    

    您只需要
    readlines
    一次

    def file2matrix(filename):
        fr = open(filename)
        lines = fr.readlines()    
        fr.close()    
        numberOfLines = len(lines)        #get the number of lines in the file
        returnMat = zeros((numberOfLines,3))       #prepare matrix to return   
        classLabelVector = []                      #prepare labels return   
        index = 0
        for line in lines:
            line = line.strip()
            listFromLine = line.split('\t')
            returnMat[index,:] = listFromLine[0:3]
            # careful here, returnMat is initialed as floats
            # listFromLine is list of strings
            classLabelVector.append(int(listFromLine[-1]))
            index += 1
        return returnMat,classLabelVector
    
    我可以提出一些其他的改变:

    def file2matrix(filename):
        with open(filename) as f:
            lines = f.readlines()
        returnList = []
        classLabelList = []
        for line in lines:
            listFromLine = line.strip().split('\t')
            returnList.append(listFromLine[0:3])
            classLabelList.append(int(listFromLine[-1]))
        returnMat = np.array(returnList, dtype=float)
        return returnMat, classLabelList
    
    甚至

    def file2matrix(filename):
        with open(filename) as f:
            lines = f.readlines()
        ll = [line.strip().split('\t')]
        returnMat = np.array([l[0:3] for l in ll], dtype=float)
        classLabelList = [int(l[-1]) for l in ll]
        # classLabelVec = np.array([l[-1] for l in ll], dtype=int)
        return returnMat, classLabelList
    

    您只需要
    readlines
    一次

    def file2matrix(filename):
        fr = open(filename)
        lines = fr.readlines()    
        fr.close()    
        numberOfLines = len(lines)        #get the number of lines in the file
        returnMat = zeros((numberOfLines,3))       #prepare matrix to return   
        classLabelVector = []                      #prepare labels return   
        index = 0
        for line in lines:
            line = line.strip()
            listFromLine = line.split('\t')
            returnMat[index,:] = listFromLine[0:3]
            # careful here, returnMat is initialed as floats
            # listFromLine is list of strings
            classLabelVector.append(int(listFromLine[-1]))
            index += 1
        return returnMat,classLabelVector
    
    我可以提出一些其他的改变:

    def file2matrix(filename):
        with open(filename) as f:
            lines = f.readlines()
        returnList = []
        classLabelList = []
        for line in lines:
            listFromLine = line.strip().split('\t')
            returnList.append(listFromLine[0:3])
            classLabelList.append(int(listFromLine[-1]))
        returnMat = np.array(returnList, dtype=float)
        return returnMat, classLabelList
    
    甚至

    def file2matrix(filename):
        with open(filename) as f:
            lines = f.readlines()
        ll = [line.strip().split('\t')]
        returnMat = np.array([l[0:3] for l in ll], dtype=float)
        classLabelList = [int(l[-1]) for l in ll]
        # classLabelVec = np.array([l[-1] for l in ll], dtype=int)
        return returnMat, classLabelList