pandas [已解决];无法将列表文件转换为行[已关闭]

r6hnlfcb  于 2023-02-17  发布在  其他
关注(0)|答案(1)|浏览(79)

编辑问题以包含desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将有助于其他人回答问题。
2天前关闭。
Improve this question
我正在使用从anomali的API来收集英特尔列表,我想问我如何才能运行代码,使其输出到Excel文件中的所有需要列标题.
因此,我创建了一个代码,在那里我拉出需要的列实现到网站。

import requests
import json
import pandas as pd
import csv

url = 'https://api.threatstream.com/api/v2/intelligence/?itype=bot_ip'

csv_columns = ['ip','source_created', 'status', 'itype', 'expiration_ts', 'is_editable', 'feed_id', 'update_id', 
               'value', 'ispublic', 'threat_type', 'workgroups', 'rdns', 'confidence', 'uuid', 'retina_confidence',
              'trusted_circle_ids', 'id', 'source', 'owner_organization_id', 'import_session_id', 'source_modified',
              'type', 'sort', 'description', 'tags', 'threatscore', 'latitude', 'modified_ts', 'org', 'asn', 
               'created_ts', 'tlp', 'is_anonymous', 'country', 'source_reported_confidence', 'can_add_public_tags',
              'subtype', 'meta', 'resource_uri']

with open("AnomaliThreat.csv","a", newline='') as filecsv:
    writer = csv.DictWriter(filecsv, fieldnames=csv_columns)
    writer.writeheader()
    
headers = {
    'Accept': 'application/json',
    'Authorization': 'apikey testing:wdwfawaf12321rfewawafa'
}

response= requests.get( url=url,headers=headers)
json_Data = json.loads(response.content)
result = json_Data["objects"]
with open("AnomaliThreat.csv","a", newline='')as filecsv:
    writer = csv.DictWriter(filecsv,fieldnames=csv_columns)
    writer.writerow(result)

如果我运行这段代码,我得到的只是'list',没有属性键,我猜是因为在响应中,列表中有一个列表,或者列表中有另一个字符串,例如
"受信任的圈ID":[一二四一四一二、二一二一四一二四一]
或者这个
"标记":[{"标识":"fwafwff"、"姓名":'wfwafwawf'},{'标识符':"31231ewfw","名称":"发电子邮件到gmail.com.wafawfawfawds.com "}],
这就是异常的React
[{"创建的源代码":无,"状态":"非活性"、"类型":"机器人IP"、"到期时间ts":""、"IP地址":"231.24124.1241.412"、"是否可编辑":错误,"源ID":23112231,"更新标识":231231,"数值":"124124124141224141","是公共的":假,"威胁类型":"机器人"、"工作组":[],"rdns":无,"信心":12,"uuid":"3123414124124142","视网膜_信心":52414,"受信任的圈子标识":[1241412、212141241],"身份证":fwaffewaefw1231231,'来源':'工作流程','所有者组织标识':2,"导入会话标识":无,"源已修改":无,"类型":"ip"、"排序":【312312424124141241,"1241414214241"】,"描述":无,"标签":[{' id ':' fwafwff ',' name ':'wfwafwawf'},{'标识符':"31231ewfw","名称":'wafwafwafwafaw@gmail.com.wafawfawfds.com'}],'威胁评分':412,"纬度":晶片/晶片,"修改的_ts":'wawafwadfd','组织':"fawfwafawe"、"asn":'fwafwa2131231','创建的_ts':"41241241241241241"、"tlp":无,"匿名"(_A):错误,"国家":"fwafw"、"来源报告的置信度":21,"可以添加公共标签":错误,"经度":- -321412,"子类型":无,"元":{"详细信息2 ":"bi2141412412342424","严重度":"3123124r3 "},"资源URI ":"/api/v2/智能/241fsdfsf241325/"},{"创建的源代码":无,"状态":"非活性"、"类型":"机器人IP"、"到期时间ts":""、"IP地址":"231.24124.1241.412"、"是否可编辑":错误,"源ID":23112231,"更新标识":231231,"数值":"124124124141224141","是公共的":假,"威胁类型":"机器人"、"工作组":[],"rdns":无,"信心":12,"uuid":"3123414124124142","视网膜_信心":52414,"受信任的圈子标识":[1241412、212141241],"身份证":fwaffewaefw1231231,'来源':'工作流程','所有者组织标识':2,"导入会话标识":无,"源已修改":无,"类型":"ip"、"排序":【312312424124141241,"1241414214241"】,"描述":无,"标签":[{' id ':' fwafwff ',' name ':'wfwafwawf'},{'标识符':"31231ewfw","名称":'wafwafwafwafaw@gmail.com.wafawfawfds.com'}],'威胁评分':412,"纬度":晶片/晶片,"修改的_ts":'wawafwadfd','组织':"fawfwafawe"、"asn":'fwafwa2131231','创建的_ts':"41241241241241241"、"tlp":无,"匿名"(_A):错误,"国家":"fwafw"、"来源报告的置信度":21,"可以添加公共标签":错误,"经度":- -321412,"子类型":无,"元":{"详细信息2 ":"bi2141412412342424","严重度":"3123124r3 "},"资源URI ":"/api/v2/智能/241fsdfsf241325/"}]
我愿意接受任何关于如何制作它的建议,以便将结果输入到Excel文件中
问题解决了!我需要在代码中添加一个值,所以我添加了这一行

csv_writer = csv.writer(data_file)

count = 0

for res in result:
    if count == 0:
        header = res.keys()
        csv_writer.writerow(header)
        count += 1
    csv_writer.writerow(res.values())
    
data_file.close()
jmo0nnb3

jmo0nnb31#

如果我没理解错的话你可以试试这样做

import requests
import json
import pandas as pd
import csv

url = 'https://api.threatstream.com/api/v2/intelligence/?itype=bot_ip'

csv_columns = ['ip','source_created', 'status', 'itype', 'expiration_ts', 'is_editable', 'feed_id', 'update_id', 
               'value', 'ispublic', 'threat_type', 'workgroups', 'rdns', 'confidence', 'uuid', 'retina_confidence',
              'trusted_circle_ids', 'id', 'source', 'owner_organization_id', 'import_session_id', 'source_modified',
              'type', 'sort', 'description', 'tags', 'threatscore', 'latitude', 'modified_ts', 'org', 'asn', 
               'created_ts', 'tlp', 'is_anonymous', 'country', 'source_reported_confidence', 'can_add_public_tags',
              'subtype', 'meta', 'resource_uri']

headers = {
    'Accept': 'application/json',
    'Authorization': 'apikey testing:wdwfawaf12321rfewawafa'
}

response= requests.get( url=url,headers=headers)
json_Data = json.loads(response.content)
result = json_Data["objects"]

dataframe_1 = pd.Dataframe

for key, value in result.items():
 if key in csv_columns:
   dataframe_1[key] = value

dataframe_1.to_csv("AnomaliThreat.csv")

沿着的东西,所以基本上在结果中迭代键、值对,检查键是否在csv_columns中,保存键值对,最后完成所有操作后,只需使用dataframe.towww.example.com_csv

相关问题