获取列表元素作为csv列值

时间:2018-11-06 05:59:20

标签: python csv

`我有一个get_values(),它以list [x0,y0]的形式返回坐标。我的列名如下。我想获取每一列的值,但它正在堆叠列x0,y0

中的所有值
fieldvalues =
["x0","y0","x1","y1","x2","y2","x3","y3","x4","y4","x5","y5","x6","y6","x7","y7","x8","y8","x9",
          "y9","x10","y10","x11","y11","x12","y12","x13","y13","x14","y14","x15","y15","x16","y16","x17","y17"]

我已经尝试过此代码

get_val = oneObject.get_values(currentFrameIndex)
for humanPos in get_val:
       fieldvalues.append(humanPos) 


with open("./pose_data.csv",mode ='w') as csvfile:
       wr = csv.writer(csvfile,dialect="excel")
       wr.writerows(fieldvalue)

get_val具有以下形式的值-列表,例如[x0,y0],依此类推,直到[x17,y17]

当前代码的输出:

x0  y0 x1 y1 x2 y2 ....x17 y17
0   1  
1   2   

所需的输出:

   x0  y0 x1 y1 x2 y2 ....x17 y17
    0  1  2  3  4  5  ....3    3
    1  2  5  5  5  6  ....4    1

感谢您的答复。谢谢。

2 个答案:

答案 0 :(得分:0)

您可以使用以下附加函数:

df = pd.DataFrame([[1, 2], [3, 4]], columns=list('AB'))
>>> df
   A  B
0  1  2
1  3  4
>>> df2 = pd.DataFrame([[5, 6], [7, 8]], columns=list('AB'))
>>> df.append(df2)
   A  B
0  1  2
1  3  4
0  5  6
1  7  8

我希望这能回答您的问题:)

答案 1 :(得分:0)

初始化为fieldValues的二级列表:

fieldvalues =[["x0","y0","x1","y1","x2","y2","x3","y3","x4","y4","x5","y5","x6","y6","x7","y7","x8","y8","x9", "y9","x10","y10","x11","y11","x12","y12","x13","y13","x14","y14","x15","y15","x16","y16","x17","y17"]]

并继续在其上附加行:

get_val = oneObject.get_values(currentFrameIndex)
fieldvalues[currentFrameIndex] = []
for humanPos in get_val:
   fieldvalues[currentFrameIndex] = fieldvalues[currentFrameIndex] + humanPos

然后在fieldvalues上进行迭代并在文件中写入行。