2014-09-28 32 views
0

我試圖把字符串集合,記號化字符串 成單個字符,並重組他們到JSON用於 構建聚類圖可視化的目的(有點像this word tree,除了字符串而不是句子)。因此,有時候字符序列會在數據之間共享(或重新共享)。構建的純文本JSON

因此,舉例來說,可以說我有一個文本文件,它看起來像:

xin_qn2 
x_qing4n3 
x_qing4nian_ 

這是我期待我的輸入;沒有CSV標題或與數據相關的任何內容。 JSON對象,然後將看起來像:

{ 
    "name": "x", 
    "children": [ 
     { 
      "name": i, 
     }, 
     { 
      "name": _, 
      "children": [ 
       { 
        "name": "q" 
       } 
      ] 
     } 
    ] 
} 

等。在將數據發送到D3.js之前,我一直在嘗試構造數據,使用Ruby將行分割成單獨的字符,但是我一直試圖弄清楚如何在層次結構JSON中構造這些數據。

file_contents = File.open("single.txt", "r") 

file_contents.readlines.each do |line| 
    parse = line.scan(/[A-Za-z][^A-Za-z]*/) 
    puts parse 
end 

我可以在瀏覽器中用d3.js代替,我只是還沒有嘗試過。

只是想知道是否有任何建議,指針或現有的工具/腳本,可能會幫助我。謝謝!

更新2014年10月2日

所以我花了一點時間在Python嘗試這一點,但我一直卡住。我現在看到,我也不正確地處理「兒童」元素。有什麼建議麼?

嘗試一個

#!/usr/bin/python 

from collections import defaultdict 
import json 

def tree(): 
    return defaultdict(tree) 

file_out = open('out.txt', 'wb') 

nested = defaultdict(tree) 

with open("single.txt") as f: 
    for line in f: 
     o = list(line) 
     char_lst = [] 
     for chars in o: 
      d = {} 
      d['name']=chars 
      char_lst.append(d) 
     for word in d: 
      node = nested 
      for char in word: 
       node = node[char.lower()] 
       print node 

print(json.dumps(nested)) 

嘗試兩個

#!/usr/bin/python 

from collections import defaultdict 
import json 

def tree(): 
    return defaultdict(tree) 

nested = defaultdict(tree) 

words = list(open("single.txt")) 
words_output = open("out.json", "wb") 

for word in words: 
    node = nested 
    for char in word: 
     node = node[char.lower()] 

def print_nested(d, indent=0): 
    for k, v in d.iteritems(): 
    print '{}{!r}:'.format(indent * ' ', k) 
    print_nested(v, indent + 1) 

print_nested(nested) 
+0

您需要製作一堆字典,然後將它們存儲在列表中。我不能說Ruby,但是Python使這非常簡單。 – 2014-09-28 22:57:32

回答

1

就快與嘗試#2。添加json.dumps(nested)到最後會打印以下JSON:

{ 
    "x":{ 
    "i":{ 
     "n":{ 
     "_":{ 
      "q":{ 
      "n":{ 
       "2":{ 

       } 
      } 
      } 
     } 
     } 
    }, 
    "_":{ 
     "q":{ 
     "i":{ 
      "n":{ 
      "g":{ 
       "4":{ 
       "n":{ 
        "i":{ 
        "a":{ 
         "n":{ 
         "_":{ 

         } 
         } 
        } 
        }, 
        "3":{ 

        } 
       } 
       } 
      } 
      } 
     } 
     } 
    } 
    } 
} 

關閉,而不是你所需的東西。順便說一句,你還可以使用下面的函數轉換嵌套defaultdict到常規字典:

def convert(d): 
    return dict((key, convert(value)) for (key,value) in d.iteritems()) if isinstance(d, defaultdict) else d 

但是,我們仍然只有類型的字典詞典(http://stardict.sourceforge.net/Dictionaries.php下載的...)。使用遞歸,我們可以按照如下轉換爲你需要的格式:

def format(d): 
    children = [] 
    for (key, value) in d.iteritems(): 
     children += [{"name":key, "children":format(value)}] 
    return children 

最後,讓我們打印出JSON:

print json.dumps(format(convert(nested))) 

這將打印以下JSON(格式爲清楚起見):

[ 
    { 
    "name":"x", 
    "children":[ 
     { 
     "name":"i", 
     "children":[ 
      { 
      "name":"n", 
      "children":[ 
       { 
       "name":"_", 
       "children":[ 
        { 
        "name":"q", 
        "children":[ 
         { 
         "name":"n", 
         "children":[ 
          { 
          "name":"2", 
          "children":[ 

          ] 
          } 
         ] 
         } 
        ] 
        } 
       ] 
       } 
      ] 
      } 
     ] 
     }, 
     { 
     "name":"_", 
     "children":[ 
      { 
      "name":"q", 
      "children":[ 
       { 
       "name":"i", 
       "children":[ 
        { 
        "name":"n", 
        "children":[ 
         { 
         "name":"g", 
         "children":[ 
          { 
          "name":"4", 
          "children":[ 
           { 
           "name":"n", 
           "children":[ 
            { 
            "name":"i", 
            "children":[ 
             { 
             "name":"a", 
             "children":[ 
              { 
              "name":"n", 
              "children":[ 
               { 
               "name":"_", 
               "children":[ 

               ] 
               } 
              ] 
              } 
             ] 
             } 
            ] 
            }, 
            { 
            "name":"3", 
            "children":[ 

            ] 
            } 
           ] 
           } 
          ] 
          } 
         ] 
         } 
        ] 
        } 
       ] 
       } 
      ] 
      } 
     ] 
     } 
    ] 
    } 
] 

下面是完整的代碼:

#!/usr/bin/python 

from collections import defaultdict 
import json 

def tree(): 
    return defaultdict(tree) 

nested = defaultdict(tree) 

words = open("single.txt").read().splitlines() 
words_output = open("out.json", "wb") 

for word in words: 
    node = nested 
    for char in word: 
     node = node[char.lower()] 

def convert(d): 
    return dict((key, convert(value)) for (key,value) in d.iteritems()) if isinstance(d, defaultdict) else d 

def format(d): 
    children = [] 
    for (key, value) in d.iteritems(): 
     children += [{"name":key, "children":format(value)}] 
    return children 

print json.dumps(format(convert(nested))) 
+0

謝謝,OrionMelt!太棒了。非常感激。 – 2014-10-03 16:44:28