我正在尝试将.csv文件(OpenFace项目的输出)转换为.pts文件,并且我想从第一行(.csv文件)中提取一些列并将其写入.pts文件
我该怎么做?
import csv
import os
from collections import defaultdict
columns = defaultdict(list) # each value in each column is appended to a list
directory = "D:\\dtsface\\"
#for filename in os.listdir(directory):
# if filename.endswith(".csv"):
with open("image_0001.csv","r",newline='') as f:
file= open("image_0001.pts","w")
reader = csv.reader(f)
row1= next(reader)
row2= next(reader)
print(row2)
file.write("n_points: ")
file.writelines("{")
i=0
j=28
while(i<=7):
file.writelines('\n\neye_lmk_x_'+str(i)+"\n\n"+str(columns[' eye_lmk_x_'+str(i)]))
i=i+1
while(j<=35):
file.writelines('\n\neye_lmk_x_'+str(j)+"\n\n"+str(columns[' eye_lmk_x_'+str(j)]))
j=j+1
'''
for filename in os.listdir(directory):
if filename.endswith(".csv"):
filename2= os.path.basename(f.name)
file= f.rsplit('.',1)[0]
with open(filename2+".pts",'w'):
while(i<=7):
file.writelines('\n\neye_lmk_x_'+str(i)+"\n\n"+str(columns[' eye_lmk_x_'+str(j)]))
i=i+1
while(j<=35):
file.writelines('\n\neye_lmk_x_'+str(j)+"\n\n"+str(columns[' eye_lmk_x_'+str(j)]))
j=j+1
file.close()
'''
我具有.csv文件的示例:
我要生成的.pts文件的示例:
答案 0 :(得分:0)
根据对该问题的评论:
@Ralf我的问题是如何提取csf文件的firs行的某些列?
如果您有此代码
with open("image_0001.csv","r",newline='') as f:
reader = csv.reader(f)
row1= next(reader)
row2= next(reader)
print(row2)
然后row2
应该是一个列表,其中每个元素都保存秒行的一个数据单元格的内容(数据类型为str
),row1
将保存第一行中的数据,依此类推。
要获取第二行的第一个单元格,只需访问第一个元素:
# first cell of first row
print(row1[0])
# first cell of second row
print(row2[0])
# third cell of second row
print(row2[2])
这能回答您的问题吗?
答案 1 :(得分:0)
我修改了我的代码,发现了您写的一个不好的解决方案,但是并没有解决某些问题, 我说一个不好的解决方案是因为每次我都应该在代码中找到要写入的列数,所以这不是一个好的解决方案,因为我想按列名来获取单元格,例如:
row2=next(reader)
print(row2['eye_lmk_x_0']) # I know that this doesn't exist but i search a something like this
代替
row2= next(reader)
print(row2[1])
这是我更新后的代码:
import csv
import os
from collections import defaultdict
columns = defaultdict(list) # each value in each column is appended to a list
directory = "D:\\dtsface\\"
#for filename in os.listdir(directory):
# if filename.endswith(".csv"):
with open("image_0001.csv","r",newline='') as f:
file= open("image_0001.pts","w")
reader = csv.reader(f)
row1= next(reader)
row2= next(reader)
print(row2[1])
print(row2[2])
file.write("n_points: ")
file.writelines("{")
i=11
j=39
while(i<=18):
#file.writelines('\n\neye_lmk_x_'+str(i)+"\n\n"+str(columns[' eye_lmk_x_'+str(i)]))
file.writelines('\n'+str(row2[i]))
i=i+1
while(j<=46):
#file.writelines('\n\n'+str(j)+"\n\n"+str(columns[' eye_lmk_x_'+str(j)]))
file.writelines('\n'+str(row2[j]))
j=j+1
'''
for filename in os.listdir(directory):
if filename.endswith(".csv"):
filename2= os.path.basename(f.name)
file= f.rsplit('.',1)[0]
with open(filename2+".pts",'w'):
while(i<=7):
file.writelines('\n\neye_lmk_x_'+str(i)+"\n\n"+str(columns[' eye_lmk_x_'+str(j)]))
i=i+1
while(j<=35):
file.writelines('\n\neye_lmk_x_'+str(j)+"\n\n"+str(columns[' eye_lmk_x_'+str(j)]))
j=j+1
file.close()
'''
我的输出现在是这样的:
我想获取y坐标,所以我的问题是如何通过列名而不是列号来获取行?