想用python处理csv文件密码相同的数据
类似这样的两列数据
ip | 密码 |
---|---|
172.16.1.4 | passwd |
172.16.1.41 | pass23 |
172.16.1.42 | passwd |
172.16.1.43 | 1234 |
172.16.1.44 | 1234 |
我想要达到的结果
passwd:172.16.1.4, 172.16.1.42
1234 :172.16.1.43,172.16.1.44
类似这样的两列数据
ip | 密码 |
---|---|
172.16.1.4 | passwd |
172.16.1.41 | pass23 |
172.16.1.42 | passwd |
172.16.1.43 | 1234 |
172.16.1.44 | 1234 |
passwd:172.16.1.4, 172.16.1.42
1234 :172.16.1.43,172.16.1.44
with open('test.csv') as f:
lines = f.readlines()
data = {}
for line in lines:
line = line[:-1]
ip = line.split(',')[0]
passwd = line.split(',')[1]
ips = data.get(passwd, [])
ips.append(ip)
data[passwd] = ips
for key, value in data.items():
print(f"{key}:{','.join(value)}")
csv文件如下: