2017-06-21 8 views
0

私はoutput.txtという名前のファイルのIPアドレスのリストの場所を調べるためにip2locationを使用しています。別のファイルip_info.txtに書き込みます私のファイルのIPアドレスがUSであるエントリ。以下はこれを行うための私のコードです。特定の場所のIPアドレスを見つけるためのip2location

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

ああ私はPythonに新しいのです...ありがとう4 d答え。 – salim

関連する問題