2014-01-14 73 views
2

我有這樣的代碼:用Python解釋工作,文件

def merge_new_old_urls(urls_list, urls_file_path): 
    url_dict = {} 
    try: 
     with open(urls_file_path, "r") as f: 
      data = f.readlines() 
     for line in data: 
      #read what is already in file 
      url_dict = { line.split()[0]: int(line.split()[1])} 
     for new in urls_list: 
      for key in url_dict.keys(): 
       if new == key: 
        print 'found' 
        url_dict[key] += 1 
       else: 
        url_dict[new] = 1 

    except IOError: 
     logging.critical('no files to read from %s' % urls_file_path) 
     raise IOError('no files to read from %s' % urls_file_path) 
    return url_dict 

這應該從文件中讀取數據,並用新的數據表進行合併,計算它是重複了多少遍。與舊網址的文件看起來是這樣的:

http://aaa.com 1 
http://bbb.com 2 
http://ccc.com 1 

如果URL的新清單中包含http://aaa.comhttp://bbb.com的字典應該是:

'http://aaa.com':2 
'http://bbb.com':3 
'http://ccc.com':1 

但我的代碼不工作的權利。有人可以治療嗎?

+2

但我的代碼不工作的權利...什麼是錯的?????? –

+0

請定義「不適用」。它產生了錯誤的輸出,沒有輸出,例外...... –

+0

爲什麼你要比較字典中的所有鍵?可能字典有一個快速查找方法? – Mark

回答

2

您重新定義url_dict每一次循環:

url_dict = {line.split()[0]: int(line.split()[1])} 

添加條目到詞典中,而不是:

for line in data: 
    key, val = line.split() 
    if key in url_dict: 
     url_dict[key] += val 
    else: 
     url_dict[key] = val 

並通過字典搜索是完全沒有必要,你可以使用相同的語法如上:

for key in urls_list: 
    if key in url_dict: 
     url_dict[key] += val 
    else: 
     url_dict[key] = val 

最後,你的肩膀dn't裹了這麼多的try

try: 
    with open(urls_file_path, "r") as f: 
     data = f.readlines() 
except IOError: 
    logging.critical('no files to read from %s' % urls_file_path) 
    raise IOError('no files to read from %s' % urls_file_path) 
else: 
    # rest of your code 
+0

非常感謝,幫助 – user3156971