Python IP2仅用于查找特定位置的ip地址的位置
我使用ip2location在一个名为output.txt的文件中查找ip地址列表的位置,并将答案写入另一个文件ip_info.txt。我只想在我的文件中写入ip地址为US的条目。下面是我的代码Python IP2仅用于查找特定位置的ip地址的位置,python,ip2location,Python,Ip2location,我使用ip2location在一个名为output.txt的文件中查找ip地址列表的位置,并将答案写入另一个文件ip_info.txt。我只想在我的文件中写入ip地址为US的条目。下面是我的代码 import IP2Location; import urllib; import time; start_time = time.time() IP2LocObj = IP2Location.IP2Location(); IP2LocObj.open("data/IP-COUNTRY-REGION-
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
如果(rec.country\u short=='US')无效,您可以查看,以供参考
你是说:
如果rec.country\u short='US':
?哦,我对python有点陌生……谢谢你的回答。