简体   繁体   English

CSV文件解析(python)

[英]CSV file parsing (python)

I am having some issues parsing a csv file with 14 columns. 我在解析具有14列的csv文件时遇到一些问题。

for row in training_set_data:
    if skiprow:
            skiprow = False
    else:
            for r in range(len(row)):
                    row[r] = float(row[r])
            training_set.append(row)

this seems to be working to just get a list of the vectors, but the next thing I want to do is collect the first 13 entries in each row and make one set of vectors, and then collect the last column and make a separate set of vectors of that. 这似乎只是获取向量列表而已,但是我要做的下一件事是收集每行的前13个条目并创建一组向量,然后收集最后一列并单独创建一组的向量。 My code currently looks like this for the 13 entry vectors: 对于13个进入向量,我的代码当前如下所示:

def inputVector(inputs):
    for r in inputs:
        inputs.pop(13)
    return inputs

This is not working and when I go to print it, it is still 14 entries long. 这不起作用,当我打印时,它仍然有14个条目。 Can anyone tell me what I am doing wrong? 谁能告诉我我在做什么错? Sorry if the question doesn't make too much sense, I am pretty new to coding. 抱歉,如果这个问题没有太大意义,我对编码非常陌生。

Edit: First 11 lines of the csv file and the call to input vecto 编辑:csv文件的前11行和输入vecto的调用

53,1,3,130,197,1,2,152,0,1.2,3,0,3,0
42,1,4,136,315,0,0,125,1,1.8,2,0,6,1
46,1,4,140,311,0,0,120,1,1.8,2,2,7,1
42,1,4,140,226,0,0,178,0,0,1,0,3,0
54,1,4,140,239,0,0,160,0,1.2,1,0,3,0
67,0,3,115,564,0,2,160,0,1.6,2,0,7,0
65,0,3,140,417,1,2,157,0,0.8,1,1,3,0
56,0,4,134,409,0,2,150,1,1.9,2,2,7,1
65,0,3,160,360,0,2,151,0,0.8,1,0,3,0
57,0,4,120,354,0,0,163,1,0.6,1,0,3,0
55,0,4,180,327,0,1,117,1,3.4,2,0,3,1

inputV = inputVector(training_set)

The problem is this code: 问题是此代码:

def inputVector(inputs):
    for r in inputs:
        inputs.pop(13)
    return inputs

You're iterating over all inputs , and removing elements from inputs rather than from r . 您要遍历所有inputs ,并从inputs而不是r删除元素。 To remove element 13 from each row , do this instead: 要从每一行中删除元素13,请执行以下操作:

def inputVector(inputs):
    for r in inputs:
        r.pop(13)  # <-- replaced inputs with r
    return inputs

Try something like this: 尝试这样的事情:

first_13s = []
last_1s = []

for r in inputs:
    first_13s.append(r[:13])
    last_1s.append(r[13])

also you can replace a number of lines in your first block of code just by using training_set_data[1:] 您也可以仅使用training_set_data [1:]替换第一行代码中的许多行

python list slicing is very handy Explain Python's slice notation python列表切片非常方便解释Python的切片符号

also you can use list comprehensions for your float conversion: 您还可以使用列表推导进行浮点转换:

for r in range(len(row)):
    row[r] = float(row[r])

becomes 变成

row = [float(r) for r in row]

so the first block can be done like this: 所以第一个步骤可以这样完成:

for row in training_set_data[1:]:
    row = [float(r) for r in row]
    training_set.append(row)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM