请求返回的文本字符串不会ast.literal_eval列出

时间:2018-12-29 18:35:13

标签: python python-requests abstract-syntax-tree

调用带有请求的API来查找代码说明。 API返回的字符串类似于列表。我在使用ast.literal_eval将字符串转换为列表时遇到麻烦。我尝试了多种响应类型(.text,.raw等)的变体以及map,str等的变体来转换字符串。希望将响应传递到任何可用的Python对象中。

    HANDLE process = OpenProcess(PROCESS_ALL_ACCESS, false, pid);

if (process == NULL)
{
    std::cout << "Error opening process." << std::endl;
    return false;
}

const char * dllString = "C:\\test.dll";

// load memory for dll

int bytes = sizeof(dllString);


PVOID mem = VirtualAllocEx(process, NULL, sizeof(dllString) + 1, MEM_COMMIT, PAGE_READWRITE);

if (mem == NULL)
{
    std::cout << "Unable to allocate mem." << std::endl;
    CloseHandle(process);
    return false;
}

// write dll path to that location
SIZE_T bytesWritten;
BOOL status = WriteProcessMemory(process, mem, dllString, sizeof(dllString) + 1, &bytesWritten);

if (!status)
{
    std::cout << "Writing dll path failed." << std::endl;
    VirtualFreeEx(process, mem, sizeof(dllString) + 1, MEM_RELEASE);
    CloseHandle(process);
    return false;
}

FARPROC loadLibrary = GetProcAddress(GetModuleHandle("kernel32.dll"), "LoadLibraryA");

HANDLE thread = CreateRemoteThread(process, NULL, NULL, reinterpret_cast<LPTHREAD_START_ROUTINE>(loadLibrary), mem, NULL, NULL);

if (thread == INVALID_HANDLE_VALUE)
{
    std::cout << "Unable to create thread in remote process. " << std::endl;
    VirtualFreeEx(process, mem, sizeof(dllString) + 1, MEM_RELEASE);
    CloseHandle(process);
}


WaitForSingleObject(thread, INFINITE);

DWORD exitCode = 0;
GetExitCodeThread(thread, &exitCode);

if (exitCode != 0)
    std::cout << "DLL loaded successfully." << std::endl;
else
    std::cout << "DLL loading failed." << std::endl;

CloseHandle(thread);
VirtualFreeEx(process, mem, sizeof(dllString) + 1, MEM_RELEASE);
CloseHandle(process);
return true;

响应为: [1,[“ W59.21XS”],null,[[“ W59.21XS”,“被龟咬伤,后遗症”]]]

但如果我添加:

import requests
import ast

burl = "https://clinicaltables.nlm.nih.gov/" \
"api/icd10cm/v3/search?sf=code,name&maxList=1&terms="

dotted_code = "W59.21XS"

r = requests.get(burl+dotted_code).text

print(r)

我得到:

文件“ D:\ Local \ Continuum \ anaconda3 \ lib \ ast.py”,第55行,_convert_num     引发ValueError('格式错误的节点或字符串:'+ repr(node))

ValueError:格式错误的节点或字符串:<_ast.Name对象,位于0x0000015A01B670B8>

将返回文本转换成可用的Python对象的更好方法?

2 个答案:

答案 0 :(得分:3)

那是JSON:

>>> import json
>>> json.loads("""[1,["W59.21XS"],null,[["W59.21XS","Bitten by turtle, sequela"]]]""")
[1, ['W59.21XS'], None, [['W59.21XS', 'Bitten by turtle, sequela']]]

答案 1 :(得分:3)

为什么 literal_eval不起作用的原因是因为null在Python中不存在,所以literal_eval不知道如何处理。如果将null更改为None(或更改为字符串'null'),那么literal_eval将会起作用:

>>> literal_eval('''[1,["W59.21XS"], None, [["W59.21XS","Bitten by turtle, sequela"]]]''')
[1, ['W59.21XS'], None, [['W59.21XS', 'Bitten by turtle, sequela']]]

您可以使用response.json

>>> r = requests.get('https://clinicaltables.nlm.nih.gov/api/icd10cm/v3/search?sf=code,name&maxList=1&terms=W59.21XS')
>>> r.json()
[1, ['W59.21XS'], None, [['W59.21XS', 'Bitten by turtle, sequela']]]
#                 ^ Note that null was automatically converted to None