2015-08-21 22 views
1

我有一個json文件與對象和一個文本文件與幾個組(每組有5個數字,我有他們在列表中這樣:每個組的第一個數字在列表1中,每個組的第二個數字在列表2中等等)。我基本上必須將json的每個對象與我創建的每個組進行匹配。問題在於Im得到了來自Json的最後一個元素。文本文件中的組以正確的方式創建。循環不工作,當我嘗試閱讀一個JSON文件和一個文本文件與python

這是我的代碼:

import json 

NUM_LIST = 5 
index = 0 

def report(a, b, c, d, e, index): 
    json_file = 'json_global.json' 
    json_data = open(json_file) 
    data = json.load(json_data) 

    i = 0 
    index = 0 
    item = 0 
    cmd = " " 
    ind = 0 

    for node in data: 
     for i in range(0, 5): 
      item = data[i]['item'] 
      cmd = data[i]['command'] 

     index+= 1 

    print item, cmd, a, b, c, d, e 

f = open("Output.txt", "r") 
lines = [line.rstrip() for line in f if line != "\n"] 

NUM_LISTS = 5 
groups = [[] for i in range(NUM_LISTS)] 
listIndex = 0 

for line in lines: 
    if "Transactions/Sec for Group" not in line: 
     groups[listIndex].append(float(line)) 
     listIndex += 1 
     if listIndex == NUM_LISTS: 
      listIndex = 0 

value0 = groups[0] 
value1 = groups[1] 
value2 = groups[2] 
value3 = groups[3] 
value4 = groups[4] 

for i in range(0, 5): 
    a = value0[i] 
    b = value1[i] 
    c = value2[i] 
    d = value3[i]  
    e = value4[i] 
    i += 1 

    report(a, b, c, d, e, index) 

JSON文件看起來像:

[ 
    { 
    "item": 1, 
    "command": "AA" 
    }, 
    { 
    "item": 2, 
    "command": "BB", 
    }, 
    { 
    "item": 3, 
    "command": "CC", 
    }, 
    { 
    "item": 4, 
    "command": "DD", 
    }, 
    { 
    "item": 5, 
    "command": "EE", 
    } 
] 

文本文件看起來像這樣:

Transactions/Sec for Group = AA\CODE1\KK 
1011.5032 
2444.8864 
2646.6893 
2740.8531 
2683.8178 
Transactions/Sec for Group = BB\CODE1\KK 
993.2360 
2652.8784 
3020.2740 
2956.5260 
3015.5910 
Transactions/Sec for Group = CC\CODE1\KK 
1179.5766 
3271.5700 
4588.2059 
4174.6358 
4452.6785 
Transactions/Sec for Group = DD\CODE1\KK 
1112.2567 
3147.1466 
4014.8404 
3913.3806 
3939.0626 
Transactions/Sec for Group = EE\CODE1\KK 
1205.8499 
3364.8987 
4401.1702 
4747.4354 
4765.7614 

中的人體邏輯程序工作正常。這些組似乎沒問題,但不是從Json文件中獲得1到5的列表,而是使用5號命令EE顯示所有內容。相反應該出現:項目1,2,3,4,5與他們的命令

我的清單1將有數字:1011.5032,993.2360,1179.5766,1112.2567,1205.8499。 我的目錄2將擁有的數字:2444.8864,2652.8784,3271.5700,3147.1466, 我使用的Python版本是2.6

回答

0

基於你的解釋,很難說你想做什麼 - 你意味着下面的嵌套循環?內循環執行5次,但在每次迭代中,它將覆蓋之前的值itemcmd

for node in data: 
    for i in range(0, 5): 
     item = data[i]['item'] 
     cmd = data[i]['command'] 

    index+= 1 

嘗試打印每一個內循環執行時間值:

for node in data: 
    for i in range(0, 5): 
     item = data[i]['item'] 
     cmd = data[i]['command'] 
     print item, cmd 


    index+= 1 
+0

打印的唯一值是最新的值...我試圖從我的json文件中獲得第一個元素,並使用從我的.txt文件創建的第一個列表(我從我的文本文件創建的第一個組是第一個文本文件中每個組的5個數字) –

0

我覺得這個代碼是你的問題:

for node in data: 
    for i in range(0, 5): 
     item = data[i]['item'] 
     cmd = data[i]['command'] 

項目將永遠是「5」,並命令將執行完成後始終爲「EE」。也許你的縮進是關閉它下面的代碼,並且該代碼應該在循環內?

+0

總是進入項目編號5.這是正確的。那是我遇到問題的地方。我怎麼能修復這個循環? –

+0

將打印語句直接移動到「cmd =」行並縮進,使其與「item =」和「cmd =」行對齊,然後您將看到打印的答案。一旦循環結束,只要你有一個不再縮進的語句,所以你所擁有的循環根本就沒有做任何事情 - 循環中沒有任何內容被包含在循環中,因爲它縮進了左邊。 – swmcdonnell