2016-04-09 35 views
-2

ile satır sonları eklenir. Böylece, komutumun çalışması gereken bir .json dosyası var. Sorun şu ki, script'im giriş dosyası boyunca hareket etmek için linereader kullanıyor ve sahip olduğum json dosyası tek bir büyük satırda. Dosyaya baktığımda, yeni satırların nerede oluşturulacağını bulmak için neler yapabileceğimi bilmiyorum. Bunu nasıl yapabileceğimi bilen var mı? Projemde bu .json dosyasını (aşağıda bir örnek) alabilmem, doğru bir şekilde biçimlendirilmiş (veya en azından yeni satırlarda) yapabilmem ve daha sonra bunu komut dosyasının girdisi olarak okuyabileceğim için hayati önem taşımaktadır. Şimdiden teşekkürlerBir .json dosyası nasıl okunur ve yeni bir dosyaya yazılır, Python

Örnek 1, şimdiki yolu .json dosyası arar:

{"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":[]} 

Örnek 2, benim komut dosyası ile iyi çalışır biçimi: büyük takdir

{ 
    "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":[ 

    ] 
} 

Herhangi bir yardım . Projemin tamamından önce programımın tamamını yeniden yazmak için zamanım yok, bu yüzden bu şekilde yapılmalı ya da orijinal senaryona önemli yeniden yazma gerektirmeyen benzer bir yöntem.

+0

şu anda bu yüzden herhangi bir sorun tespit edebilirsiniz kullandığınız kod gönderin. –

+0

Geçerli JSON için bu biçimlendirme gerekli değildir; Belki de kendi yuvarlanmadan ziyade uygun bir ayrıştırıcı kullanmalısın? – jonrsharpe

+3

Eğer betiğiniz geçerli bir JSON'u takarsa, bu oldukça basılmış bir şey değildir ** bununla ** çok yanlıştır ve bunu güzel yazdırma yerine düzeltmeniz gerekir. Neden komut dosyanız JSON ayrıştırmak için bir JSON ayrıştırıcısı kullanmıyor? Ve sadece FYI, bunun bir üniversite ödevi olması durumunda: JSON'u ayrıştıramayacak bir JSON ayrıştırıcısı kullandığınız için büyük olasılıkla başarısız oldum;) – ThiefMaster

cevap

1

Bunu bir deneyin.

Girdi:

import json 

compactJSON = """{"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":[]}""" 

dictionary = json.loads(compactJSON) 

prettyJSON = json.dumps(dictionary, indent=4, separators=(',', ': ')) 

print(prettyJSON) 

Çıktı:

{ 
    "edges": [ 
     { 
      "fromID": "119927", 
      "edgeID": "160906", 
      "formEdgeID": null, 
      "toID": "119932" 
     }, 
     { 
      "fromID": "119932", 
      "edgeID": "160907", 
      "formEdgeID": null, 
      "toID": "119931" 
     }, 
     { 
      "fromID": "119928", 
      "edgeID": "160908", 
      "formEdgeID": null, 
      "toID": "119933" 
     }, 
     { 
      "fromID": "119933", 
      "edgeID": "160909", 
      "formEdgeID": null, 
      "toID": "119927" 
     }, 
     { 
      "fromID": "119929", 
      "edgeID": "160910", 
      "formEdgeID": null, 
      "toID": "119934" 
     }, 
     { 
      "fromID": "119934", 
      "edgeID": "160911", 
      "formEdgeID": null, 
      "toID": "119932" 
     }, 
     { 
      "fromID": "119930", 
      "edgeID": "160912", 
      "formEdgeID": null, 
      "toID": "119934" 
     } 
    ], 
    "locutions": [], 
    "nodes": [ 
     { 
      "timestamp": "2015-12-14 12:09:13", 
      "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:14", 
      "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" 
     } 
    ] 
} 
İlgili konular