#打开源文件
reader_csv = pd.read_table(file_path,iterator=True,names=field_name)
loop = True
row_csv_data = []
while loop:
try:
#分批获取数据
chunk = reader_csv.get_chunk(chunkSize)
#遍历分批数据中的每一行
for row in chunk.iterrows():
row_list = row[1]["saddr"].split(",")
list1 = [1,2,3,4]
#如果IP字段存在,则查询geolite的地理位置
if row_list[1]:
ip_address = row_list[1]
#row_csv_data.append(row_list+list1)
geolite_data = ip_get_location(ip_address)
if geolite_data is not None:
#print(type(geolite_data))
row_csv_data.append(row_list+geolite_data)
except Exception as err:
loop = False
print("loop is stoped! {}".format(err))
with open(new_file_path,'w',newline='')as f:
f_csv = csv.writer(f)
f_csv.writerows(row_csv_data)
【无标题】
最新推荐文章于 2025-12-02 19:01:55 发布
该博客讲述了如何使用Python的pandas库分批读取CSV文件,并通过IP地址查询地理位置信息。在处理过程中,遇到异常会停止循环,并将处理后的数据写入新的CSV文件。内容涉及到IP查询、数据拼接以及文件写入操作。
8786

被折叠的 条评论
为什么被折叠?



