2017-06-20 96 views
1

我探討了其他問題,我並不驚訝地發現沒有滿足我的需求的答案。Python 2.6導出字典多行到Python文件字典格式(.py文件)

我吸收了一個現有的項目,需要對其進行一些改進。一個完整的重寫是在將來很遠,並且目前還不能改變文件格式,因此我需要一個解決方法。

數據源是一個數據庫,帶有一個鍵/值對字段。

我正在選擇數據庫並導出。

問題:

我們正在創建一個新的字典,然後解析它。

output = [] 
output.append("dictionary = {" + "\n") 
# dbdata is just a dictionary of data from the database containing key and value 
for result in dbdata 
    output.append("\"" + str(result['key']) + "\" : \"" + str(result['value']) + "\",") 
output.append("}") 
response = "\n".join(output) 

我們現在有一個非常大的字典將被打印出來。

當前系統的輸出是這樣的:

dictionary = { 
    "one": "one value", 
    "two": "two value", 
    'three': ' 
    12345/54321*23:Some-Magic/Stuff 
    12345/54331*23:Some-Magic/Stuff 
    12345/54321*21:Some-Magic/Stuff 
    12345/54321*53:Some-Magic/Stuff 
    12341/54321*23:Some-Magic/Stuff 
    12343/54321*23:Some-Magic/Stuff 
    12347/54321*23:Some-Magic/Stuff 
    12145/54321*23:Some-Magic/Stuff', 
    "four":"four value", 
    'five': "['this','is','another']", 
} 

所需的格式會是什麼樣子:

dictionary = { 
    "one": "one value", 
    "two": "two value", 
    "three": """ 
    12345/54321*23:Some-Magic/Stuff 
    12345/54331*23:Some-Magic/Stuff 
    12345/54321*21:Some-Magic/Stuff 
    12345/54321*53:Some-Magic/Stuff 
    12341/54321*23:Some-Magic/Stuff 
    12343/54321*23:Some-Magic/Stuff 
    12347/54321*23:Some-Magic/Stuff 
    12145/54321*23:Some-Magic/Stuff""", 
    "four":"four value", 
    "five":['this','is','another'], 
} 

注意:您會看到,在現有的,它是:

  • 引用鍵「五」當它不應該
  • 不應該三重引用「三」,因爲它應該 - 一個多行值。

說明爲什麼這不是一個重複:

  • 格式不能更改到一個更合理的/健全的格式,即CSV,JSON,陽明海運 - 所有其他線程(很正確)建議改變它。
  • 必須保持人類可讀性多行格式

是的,我知道它的荒謬的,因爲它不是一個非常人性化的可讀格式。

對不起,很長的文字,這是一個難以解釋的問題 - 我意識到這整個事情是糟糕的設計。

謝謝任何​​能夠幫助的人。

+0

目前我還不清楚你在做什麼。什麼是'export = [「dictionary = {」,(above_data_here_from_database),'}']'假設是?什麼是'above_data_here_from_database'?一個元組?包含字符串?你能舉個例子嗎? –

+0

這個問題並不是很清楚,但可以[\ [Python \]:pprint.pprint](https://docs.python.org/2/library/pprint.html#pprint.pprint)help ?如果是,請使用'pprint.pformat'將其存儲在一個字符串中,然後將該字符串寫入某個文件。無論如何,請向我們展示一下它是如何的,以及您希望如何。 – CristiFati

+0

@ juanpa.arrivillaga - 不,正在創建一個數組,我們嵌入字典「dictionary = {」的第一個組件,然後將數據作爲key:value插入。對困惑感到抱歉。 –

回答

0

我想通了 - 至少對於我自己的用例。

爲清楚起見,我執行以下操作:

  • 檢查JSON
  • 檢查Python列表
  • 如果字符串,引號,
  • 如果多行,用三引號

下面是解決我的問題的代碼 - 當然,如果有任何更好的方法建議,我很想知道。

import ast 
import json 


dictionary = { 
    "one": "one value", 
    "two": "two value", 
    "three": """ 
    12345/54321*23:Some-Magic/Stuff 
    12345/54331*23:Some-Magic/Stuff 
    12345/54321*21:Some-Magic/Stuff 
    12345/54321*53:Some-Magic/Stuff 
    12341/54321*23:Some-Magic/Stuff 
    12343/54321*23:Some-Magic/Stuff 
    12347/54321*23:Some-Magic/Stuff 
    12145/54321*23:Some-Magic/Stuff""", 
    "four": "four value", 
    "five": "['this','is','another']", 
} 


def export_python_formatting(input): 
    output = "config = { \n" 
    evalVal = "" 

    for key, value in input.items(): 
     isJSON = False 
     output += " " 
     key = str(key) 


     # See if the value is correct JSON 

     try: 
      z = json.loads(value) 
      # Valid JSON - This might be a possible datatype in my use case. 
      output += '"' + key + '"' + ":" + "'''" + value + "'''" 
      evalVal = value 
      isJSON = True 
     except: 
      # Invalid JSON - So let's use literal_eval to see if it contains a list 
      try: 
       evalVal = ast.literal_eval(value) 
      except SyntaxError: 
       evalVal = value 
      except ValueError: 
       print("There was a value error with " + key) 

     varType = type(evalVal) 

     # print(varType) 
     if varType is str: 
      if "\n" in value: 
       output += '"' + key + '"' + ":" + "'''" + value + "'''" 
      else: 
       if isJSON is False: 
        output += '"' + key + '"' + ":" + '"' + value + '"' 
     else: 
      output += '"' + key + '"' + ":" + str(value) 

     output += ",\n" 

    output += "}" 
    return output 


x = export_python_formatting(dictionary) 
print(x) 

謝謝大家誰幫忙。