-2
因此,我有一個.json文件,我的腳本需要使用它。問題是我的腳本正在使用線路閱讀器來瀏覽輸入文件,而我擁有的json文件全部放在一個巨大的單行上。看着這個文件,我不確定我能做些什麼來找到在哪裏創建新行。有誰知道我會怎麼做?對於我的項目來說,至關重要的是我可以使用這個.json文件(下面的示例),使其格式正確(或至少在新行中),然後將其作爲腳本的輸入讀取。在此先感謝如何讀取.json文件並寫入新文件,用Python插入換行符
例1,目前的方式以.json文件如下:
{"nodes":[{"nodeID":"119927","text":"Yes, it's annoying and cumbersome to separate your rubbish properly all the time.","type":"I","timestamp":"2015-12-14 12:09:13"},{"nodeID":"119928","text":"Three different bin bags stink away in the kitchen and have to be sorted into different wheelie bins.","type":"I","timestamp":"2015-12-14 12:09:14"},{"nodeID":"119929","text":"But still Germany produces way too much rubbish","type":"I","timestamp":"2015-12-14 12:09:14"},{"nodeID":"119930","text":"and too many resources are lost when what actually should be separated and recycled is burnt.","type":"I","timestamp":"2015-12-14 12:09:14"},{"nodeID":"119931","text":"We Berliners should take the chance and become pioneers in waste separation!","type":"I","timestamp":"2015-12-14 12:09:14"},{"nodeID":"119932","text":"Default Conflict","type":"CA","timestamp":"2015-12-14 12:09:14"},{"nodeID":"119933","text":"Default Inference","type":"RA","timestamp":"2015-12-14 12:09:14"},{"nodeID":"119934","text":"Default Conflict","type":"CA","timestamp":"2015-12-14 12:09:14"}],"edges":[{"edgeID":"160906","fromID":"119927","toID":"119932","formEdgeID":null},{"edgeID":"160907","fromID":"119932","toID":"119931","formEdgeID":null},{"edgeID":"160908","fromID":"119928","toID":"119933","formEdgeID":null},{"edgeID":"160909","fromID":"119933","toID":"119927","formEdgeID":null},{"edgeID":"160910","fromID":"119929","toID":"119934","formEdgeID":null},{"edgeID":"160911","fromID":"119934","toID":"119932","formEdgeID":null},{"edgeID":"160912","fromID":"119930","toID":"119934","formEdgeID":null}],"locutions":[]}
例2,我的腳本運行良好的格式:
{
"nodes":[
{
"nodeID":20,
"text":"Yes, it's annoying and cumbersome to separate your rubbish properly all the time.",
"type":"I"
},
{
"nodeID":21,
"text":"Three different bin bags stink away in the kitchen and have to be sorted into different wheelie bins.",
"type":"I"
},
{
"nodeID":22,
"text":"But still Germany produces way too much rubbish",
"type":"I"
},
{
"nodeID":23,
"text":"and too many resources are lost when what actually should be separated and recycled is burnt.",
"type":"I"
},
{
"nodeID":24,
"text":"We Berliners should take the chance and become pioneers in waste separation!",
"type":"I"
},
{
"nodeID":40,
"text":"Default Conflict",
"type":"CA"
},
{
"nodeID":41,
"text":"Default Inference",
"type":"RA"
},
{
"nodeID":42,
"text":"Default Conflict",
"type":"CA"
}
],
"edges":[
{
"fromID":20,
"toID":40
},
{
"fromID":40,
"toID":24
},
{
"fromID":21,
"toID":41
},
{
"fromID":41,
"toID":20
},
{
"fromID":22,
"toID":42
},
{
"fromID":42,
"toID":40
},
{
"fromID":23,
"toID":42
}
],
"schemefulfillments":[
],
"participants":[
],
"locutions":[
]
}
任何幫助是極大的讚賞。我沒有時間在我的項目結束之前完全重寫我的整個程序,所以必須以這種方式完成,或者類似的方法不需要對原始腳本進行重大改寫。
請發表您目前使用的,所以我們可以發現有任何問題的代碼。 –
對於有效的JSON,此格式不是必需的;也許你應該使用一個合適的解析器而不是滾動你自己的? – jonrsharpe
如果你的腳本在有效的JSON上被扼殺,而這些JSON不是很漂亮的東西,那麼它就很**錯了,你需要修復它而不是漂亮地打印它。爲什麼你的腳本不使用JSON解析器來解析JSON?只是供參考,如果這是一個大學作業:我可能會因爲使用無法解析JSON的JSON解析器而失敗;) – ThiefMaster