使用csv.DictWriter引用特定元素

时间:2018-07-13 08:59:52

标签: python csv dictionary

我正在使用csv.DictWriter,并且具有多个字段名,例如:

filename, filesize, attributes, coordinates, type

我需要我的最终.csv文件如下所示:

filename, filesize, attributes, coordinates, type
XXX.jpg,123,"{}","{""name"":""poly"",""all_points_x"":[0,10],""all_points_y"":[0,10]}","{""class"":""Test""}"

一堆引号让我头疼,因为我尝试了很多建议的解决方案,但都失败了。

例如,我尝试了以下建议(DataToCSV):

fill = "\"{}\""
fill = fill.replace("\"", "\"\"")
fillInQuotes = "\"" + fill + "\""
with open(paramPathSaveImgAUG + '\\TEST2.csv', 'w') as csvfile:
fieldnames = ['filename', 'filesize', 'attributes', 'coordinates', 'type']
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
    writer.writeheader()
    writer.writerow({'filename':name, 'filesize':size,'attributes':fillInQuotes,'coordinates':coor,'type':class})

attributes的结果为"""""""{}""""""" ...

所以我要寻找的是在引号中设置单个单词的方法,因此我也可以在引号中设置单个单词,例如“ class”等。

任何帮助将不胜感激!

0 个答案:

没有答案
相关问题