我正在尝试将以下数据导入CSV:
{'test.foo.com': {'domain': 'foo.com','FQDN': 'test.foo.com', 'AS': 'AS1111', 'ressource_type': 'A', \
'nb_ip': '1', 'IP': '1.1.1.1', 'service': ['UNKNOWN'], 'port': '[443, 8443]'}}
我几乎成功地使用了以下代码:
#!/bin/python3
## Import ##
# Offical
import csv
### Main ###
if __name__ == '__main__':
## Variables
csv_headers = ['domain', 'FQDN', 'AS', 'ressource_type', 'nb_ip', 'IP', 'service', 'port']
final_data = {'test.foo.com': {'domain': 'foo.com','FQDN': 'test.foo.com', 'AS': 'AS1111', 'ressource_type': 'A', \
'nb_ip': '1', 'IP': '1.1.1.1', 'service': ['UNKNOWN'], 'port': '[443, 8443]'}}
# Open the csv file in "write mode"
with open(file_name, mode='w') as file:
# Prepare the writer to add a dict into the csv file
csv_writer = csv.DictWriter(file, fieldnames=headers)
# Write the columns header into the csv file
csv_writer.writeheader()
# Write the dict into the file
for key, val in nest_dict.items():
row = {'FQDN': key}
row.update(val)
csv_writer.writerow(row)
结果是:
domain,FQDN,AS,ressource_type,nb_ip,IP,service,port
foo.com,test.foo.com,AS1111,A,1,1.1.1.1,['UNKNOWN'],"[443, 8443]"
但我想:
domain,FQDN,AS,ressource_type,nb_ip,IP,service,port
foo.com,test.foo.com,AS1111,A,1,1.1.1.1,'UNKNOWN','443'
foo.com,test.foo.com,AS1111,A,1,1.1.1.1,'UNKNOWN','8443'
看到区别了吗? 我有一个“服务”列表(这里不需要治疗)和一个“端口”列表。 如果“端口”列中有多个端口,我需要为列表中的每个端口打印新行
我很难做到这一点,因为我没有完全理解以下代码:
# Write the dict into the file
for key, val in nest_dict.items():
row = {'FQDN': key}
row.update(val)
csv_writer.writerow(row)
你能帮我一下吗
这是最后的代码
不确定这是不是最好的方式,但对我来说已经足够好了
结果输出:
不要投票支持我的答案,我把它作为知识的目的。 所有奖励都应归@bartonstanley所有
这将给出给定数据的预期结果:
(顺便说一句,原始帖子中的代码没有运行。此代码包括使其运行的编辑。)
请注意,由于引用了“port”的值(与“service”的值不同),因此它将作为字符串读入,因此必须首先将其转换为列表。如果删除[443,8443]周围的单引号,则代码的端口部分简化为:
另一个潜在问题是“服务”。它是一个列表,所以它可以有多个值吗?如果是这样,那么需要修改代码来解决这个问题
最后,我在这里展示的代码可以更具python风格,但我希望确保它对初学者来说尽可能可读。一旦它完全按照需要工作,它就可以变得更像Python
相关问题 更多 >
编程相关推荐