2017-06-20 99 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) 

谢谢大家谁帮忙。