json 파일에서 특정 데이터를 추출하기

조회수 10877회

"apistats": { "2512": { "FindFirstFileExW": 7, "NtAllocateVirtualMemory": 26, "SetFilePointer": 4, "NtCreateFile": 92, "GetFileType": 27, "NtReadFile": 29, "GetFileInformationByHandle": 19, "RegOpenKeyExA": 14, "GetFileAttributesW": 26, "NtFreeVirtualMemory": 2, "NtClose": 19 } },

json 파일의 내용을 여기에 다 넣을순없어서 위 부분에서 FindFirstFileExW 같이 api명만 뽑아내서 추출하고 싶은데 어떤 python 코드를 작성해야 할까요.. 제발 도와주세요 ㅠㅠ

  • (•́ ✖ •̀)
    알 수 없는 사용자

1 답변

  • {
    "apistats": {
                "2512": {
                    "FindFirstFileExW": 7, 
                    "NtAllocateVirtualMemory": 26, 
                    "SetFilePointer": 4, 
                    "NtCreateFile": 92, 
                    "GetFileType": 27, 
                    "NtReadFile": 29, 
                    "GetFileInformationByHandle": 19, 
                    "RegOpenKeyExA": 14, 
                    "GetFileAttributesW": 26, 
                    "NtFreeVirtualMemory": 2, 
                    "NtClose": 19
            }
        } 
    }
    
    import json
    
    with open('sample.json', 'r') as f:
        content = json.load(f)
    
    api_list = [ x for x in content['apistats']['2512'].keys()]
    
    print(api_list)
    

    이런식으로 해보세요.

    • (•́ ✖ •̀)
      알 수 없는 사용자
    • Traceback (most recent call last): File "test2.py", line 6, in api_list = [ x for x in content['apistats']['2512'].keys()] KeyError: 'apistats' 이런식으로 에러가 나는데 왜 apistats라는 key에러가 나는걸까요? ㅠ 알 수 없는 사용자 2017.10.2 13:33
    • apistats 상위에 다른 key값이 있으면 앞에 또 붙여보세요 알 수 없는 사용자 2017.10.2 13:55
    • 와 됐어요! 감사합니다!!! 알 수 없는 사용자 2017.10.2 16:11

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)