Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python IP2仅用于查找特定位置的ip地址的位置_Python_Ip2location - Fatal编程技术网

Python IP2仅用于查找特定位置的ip地址的位置

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-

我使用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-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有点陌生……谢谢你的回答。