2015-04-12 45 views
2

我試圖開發一個mapreduce程序來顯示一個文本文件中城市的最高溫度。獲取城市和溫度值到變量文件中使用的變量

我Temperatures.txt文件有這樣的格式:

City1 10

市2月12日

...

而且我已經有mapper.py文件的工作是這樣的:

import sys 

for line in sys.stdin: 
    line = line.strip() 
    print line 

但是,不僅僅是做print line,我想要做這樣的事情:

print '%s\t%s' % (city ,temperature) 

由於開發reducer.py文件,我需要這個,所以我的問題是,如果你知道我怎麼能在我的mapper.py文件,得到每一行,並把我的變量城市和溫度內城市名稱我變溫裏面,像這樣:

import sys 

for line in sys.stdin: 

    line = line.strip() 
    words = line.split() 
    for word in words: 
     city = # how can i get this? 
     temperature = # how can i get this? 
    print line 
    # so i can show the resut like this 
    print '%s\t%s' % (city ,temperature) 

回答

1

如果城市和溫度都在每一行,你需要讓他們來自 行:

import sys 

for line in sys.stdin: 
    city, temperature = line.rsplit(None, 1) 
    print '%s\t%s' % (city ,temperature) 

你也應該使用rsplit只爲比在他們的名字一個字,更多的城市分裂一次。

如果你在文件中的空行,你還需要捕捉那些:

for line in sys.stdin: 
    if line.strip(): 
     city, temperature = line.rsplit(None, 1) 
     print '%s\t%s' % (city ,temperature) 

或者使用try /除外:

import sys 

for line in sys.stdin: 
    try: 
     city, temperature = line.rsplit(None, 1) 
     print '%s\t%s' % (city ,temperature) 
    except ValueError: 
     continue 
+0

謝謝,你的解決方案工作!也感謝您的詳細解答! – UserX

+0

@UserX,沒有問題,很高興它有幫助。 –

1

你可以使用下面的代碼

import sys 
for line in sys.stdin: 
    words = line.split() 
    if len(words) < 2: 
     continue; 
    city = words[:-1] 
    city = ''.join(city) 
    temperature = words[-1] 
    print line 
    # so i can show the resut like this 
    print '%s\t%s' % (city ,temperature) 
+0

謝謝您的回答,但你的代碼,進出口具有:回溯(最近呼叫最後): 文件「mapper.py」,第4行,在 city = words [0] IndexError:列表索引超出範圍 – UserX

+2

我個人會使用'city = words [: - 1]'和'溫度=浮動(單詞[-1])'來應對多個單詞名稱的城市。這也值得檢查一下'len(words)> 1' –

+0

@Steve Barnes完全同意你的看法。我會糾正我的答案。 – kvorobiev