所以我知道这个问题可能会重复,但是我只想知道并了解如何将TSV文件转换为JSON?我尝试在任何地方搜索,但找不到线索或理解代码。
所以这不是Python代码,而是我要转换为JSON的TSV文件:
title content difficulty
week01 python syntax very easy
week02 python data manipulation easy
week03 python files and requests intermediate
week04 python class and advanced concepts hard
这是我想要作为输出的JSON文件。
[{
"title": "week 01",
"content": "python syntax",
"difficulty": "very easy"
},
{
"title": "week 02",
"content": "python data manipulation",
"difficulty": "easy"
},
{
"title": "week 03",
"content": "python files and requests",
"difficulty": "intermediate"
},
{
"title": "week 04",
"content": "python class and advanced concepts",
"difficulty": "hard"
}
]
答案 0 :(得分:1)
如果您使用的是pandas
,则可以使用带有选项orient="records"
的{{3}}方法来获取所需的条目列表。
my_data_frame.to_json(orient="records")
答案 1 :(得分:1)