我正在尝试逐行将数据从 CSV 加载到字典中。我正在尝试添加没有重复的数据:," imsi = line[1] & site= line[3]"
。如果字典中已经存在 imsi 和 site,我只想用新行聚合没有重复的值。
imsi = line[1] & site= line[3]
20190102,100000000204197,google.com,4799,1394,1,1
20190102,100000000204197,yahoo.com,11626,5635,5,4
20190102,100000000204197,yahoo.com,11625666,563345,5,12
20190102,41602323232,youtube.com,7875,5710,4,4
20190102,41602323232,,youtube.com,11,3
from datetime import datetime
from dateutil.parser import parse
from collections import Counter
import csv
with open('/home/mahmoudod/Desktop/Tareq-Qassrawi/report.txt','r') as rf:
reader = csv.reader(rf)
with open ('/home/mahmoudod/Desktop/Tareq-Qassrawi/writer.txt','w') as wf:
hashing_table = {}
connection_val= 0
connection_val_2=0
for line in reader:
key = int(line[1])
if key != hashing_table.items():
hashing_table =({'IMSI':key
,'SITE':str(line[2])
,'DATE':str(line[0])
,'linkup' :int(line[3])
,'linkdown':int(line[4])
,'count':int(line[5])
,'connection':int(line[6])
})
connection_val = connection_val + int(hashing_table.get('connection'))
print (connection_val)
else:
connection_val_2 = connection_val_2 + int(hashing_table.get('connection'))
print (connection_val_2)
答案1
在 pandas 中,您可以通过以下示例轻松实现此目的。标题应在 csv 文件的第一行提供。并将 report.txt 重命名为 report.csv。
import pandas as pd
import random
path_csv = '/home/mahmoudod/Desktop/Tareq-Qassrawi/report.csv'
df = pd.read_csv(path_csv, header=0)
df.head()