将JSON文件转换为labels.csv。

huangapple go评论51阅读模式
英文:

Convert json file into labels.csv

问题

I can help you translate the content into Chinese:

我有一个名为 labels.json 的文件,其中包含图像名称和实际值。由于库的更改,我必须修改我的 json 文件中的数据。

在 Json 文件内

{"자랑스럽다_2730052.jpg": "자랑스럽다", "만족스럽다_1299150.jpg": "만족스럽다"}

我想生成一个 labels.csv 文件,其中包含 filename 列和 words 列,labels.csv 的格式如下。

文件名        词汇
2730052.jpg   자랑스럽다

我应该如何操作?

英文:

I have labels.json file containing the image name and ground truth value. Due to the changing in library I have to modify my data inside json file

Inside Json

{"자랑스럽다_2730052.jpg": "자랑스럽다", "만족스럽다_1299150.jpg": "만족스럽다"}

I want to generate a labels.csv file that contains the filename column and words columns and the format of labels.csvlike below.

filename        words
2730052.jpg     자랑스럽다

How I can do it?

答案1

得分: 2

你可以创建一个类似{colname:[...]}的字典,然后将其转换为数据框,使用r'.+_'来替换文本,然后保存为CSV文件。

json = {"자랑스럽다_2730052.jpg": "자랑스럽다", "만족스럽다_1299150.jpg": "만족스럽다"}
df = pd.DataFrame({'filname': json.keys(), 'words': json.values()})
df['filname'] = df['filname'].replace(r'.+_', '', regex=True)
df.to_csv('labels.csv', index=False)

df:

       filname  words
0  2730052.jpg  자랑스럽다
1  1299150.jpg  만족스럽다
英文:

You can create a dict like {colname:[...]} and turn it to dataframe, replace text by r'.+_' then save to csv file.

json = {"자랑스럽다_2730052.jpg": "자랑스럽다", "만족스럽다_1299150.jpg": "만족스럽다"}
df = pd.DataFrame({'filname': json.keys(), 'words': json.values()})
df['filname'] = df['filname'].replace(r'.+_', '', regex=True)
df.to_csv('labels.csv', index=False)

df:

       filname  words
0  2730052.jpg  자랑스럽다
1  1299150.jpg  만족스럽다

huangapple
  • 本文由 发表于 2023年4月11日 10:38:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75982032.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定