2017-06-21 72 views
0

我正在使用ip2location來查找名爲output.txt的文件中的ip地址列表的位置,並將該答案寫入另一個文件ip_info.txt.I只想寫入那些IP地址爲US的文件中的條目。以下是我的代碼。ip2location僅用於查找特定位置的ip地址

import IP2Location; 
import urllib; 
import time; 
start_time = time.time() 
IP2LocObj = IP2Location.IP2Location(); 
IP2LocObj.open("data/IP-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ZIPCODE-TIMEZONE-ISP-DOMAIN-NETSPEED-AREACODE-WEATHER-MOBILE-ELEVATION-USAGETYPE.BIN"); 
t=open('ip_info','w'); 
with open('output','r') as f: # file containing ip addresses 
for line_terminated in f: 
    line= line_terminated.rstrip('\r\n'); # strip newline 
    if line: # non-blank lines 
     rec = IP2LocObj.get_all(line); 
     if(rec.country_short == 'US') 
      t.write(line); 
      t.write('\t'); 
      t.write(rec.country_short); 
      t.write('\n'); 
print("--- %s seconds ---" % (time.time() - start_time)) 

我在這裏收到以下錯誤。

File "myprogram.py", line 13 
if(rec.country_short == 'US') 
SyntaxError: invalid syntax 

僅供參考,你可以檢查出https://www.ip2location.com/developers/python

回答

1

if(rec.country_short == 'US')不是有效的Python。

您是不是要找:

if rec.country_short == 'US':

+0

哦sry我是一種新的python ...謝謝4 d答案。 – salim