双引号内的单引号(额外引号)json.dumps

问题描述 投票:0回答:0

我正在将(我的)dict 格式转换为标准 json 格式,即将您的 dict 中的项目括在双引号内(以前它们在单引号内);然而,输出的格式是:

{"1": [1147, "Button.left", "m_lc_down_xy", 1255, 1068], 
"2": [104, "Button.left", "m_lc_up_xy", 1255, 1068], 
"3": [2925, "key_pressed:", "Key.enter", null, null], 
"4": [2264, "key_pressed:", "'1'", null, null], 
"5": [721, "key_pressed:", "'0'", null, null], 
"6": [275, "key_pressed:", "'0'", null, null], 
"7": [7096, "Button.left", "m_lc_down_xy", 927, 897], 
"8": [80, "Button.left", "m_lc_up_xy", 927, 897], 
"9": [3282, "key_pressed:", "Key.ctrl_l", null, null]}

注意,字母数字键有额外的引号。

import time
import signal
from pynput import mouse, keyboard
from pynput.keyboard import Key
import json

#wtf is this variable name explained?
#m_lc_ = mouse left click
#mch_  = mouse click history



def time_now_mls():
    return round(time.time() * 1000)


is_m_lc_down = False
is_m_lc_up = False

#mouse left click pressed vars
m_lc_down_x = 0
m_lc_down_y = 0

#mouse left click released vars
m_lc_up_x = 0
m_lc_up_y = 0

#what mouse button was pressed
mbutton =''

#keep track of time between actions in milliseconds
this_time_ms = time_now_mls()
prev_time_ms = this_time_ms
wait_time_ms = 0
#print("this_time_ms: ",this_time_ms)
#print("prev_time_ms: ",prev_time_ms)
#print("wait_time_ms: ",wait_time_ms)

# store recorded events in dict
#mch = {}

# define variables and functions for keyboard event recording
is_key_down = False
is_key_up = False
key_pressed = ''
this_key_time_ms = time_now_mls()
prev_key_time_ms = this_key_time_ms
key_wait_time_ms = 0

# store recorded keyboard evetns in dict
#kch = {}

# store all events in dict
bch = {}

def on_move(x, y):
    print('Mouse pointer XY: {0}'.format((x, y)))
        
def on_click(x, y, button, pressed):
    global m_lc_down_x
    global m_lc_down_y

    global m_lc_up_x
    global m_lc_up_y

    global is_m_lc_down
    global is_m_lc_up

    global mbutton
    global this_time_ms
    global prev_time_ms
    global wait_time_ms

    this_time_ms = time_now_mls()

    mbutton = str(button)
    wait_time_ms = this_time_ms - prev_time_ms
    print('Button Pressed: ', button,' wait_time_ms: ',str(wait_time_ms) )
    if pressed:
        is_m_lc_down = True
        m_lc_down_x = x
        m_lc_down_y = y
        #print('Event triggered down:', m_lc_down_x, m_lc_down_y)
    else:        
        is_m_lc_up = True
        m_lc_up_x = x
        m_lc_up_y = y
        #print('Event triggered up:', m_lc_up_x, m_lc_up_y)
    prev_time_ms = this_time_ms

def on_press(key):
    global is_key_down
    global key_pressed
    global this_key_time_ms
    global prev_key_time_ms
    global key_wait_time_ms

    is_key_down = True
    key_pressed = key
    this_key_time_ms = time_now_mls()
    key_wait_time_ms = this_key_time_ms - prev_key_time_ms
    prev_key_time_ms = this_key_time_ms

def on_release(key):
    global is_key_up
    global key_pressed
    global this_key_time_ms
    global prev_key_time_ms
    global key_wait_time_ms

    is_key_up = True
    key_pressed = key
    this_key_time_ms = time_now_mls

def signal_handler(signal, frame):
    # stop the keyboard and mouse listeners
    m_listener.stop()
    k_listener.stop()
    json_string = json.dumps(bch, ensure_ascii=False, default=lambda x: list(x) if isinstance(x, tuple) else str(x))
    print(json_string)
    with open ('output.txt', 'w') as f:
        for key, value in bch.items():
            json_string = json.dumps(bch, ensure_ascii=False, default=lambda x: list(x) if isinstance(x, tuple) else str(x))
            f.write(json_string)
    print('RESULTS:')
    print('\nProgram stopped.')
    # exit the program
    exit(0)

# register the signal handler
signal.signal(signal.SIGINT, signal_handler)

print('Press Ctrl-C to quit.')

m_listener = mouse.Listener(on_click=on_click, on_release=on_release)
m_listener.start()
k_listener = keyboard.Listener(on_press=on_press)
k_listener.start()


try:
    
    event_id = 0
    while True:
        if is_m_lc_down:
            event_id += 1 
            print('\nIn Loop: m_lc_down:', m_lc_down_x, m_lc_down_y, ' event_id: ', event_id)
            bch[event_id] = wait_time_ms,mbutton,'m_lc_down_xy', m_lc_down_x, m_lc_down_y
            is_m_lc_down = False
                        
        if is_m_lc_up:
            event_id += 1 
            print('\nIn Loop: m_lc_up:', m_lc_up_x, m_lc_up_y, ' event_id: ', event_id)
            bch[event_id] = wait_time_ms,mbutton,'m_lc_up_xy', m_lc_up_x, m_lc_up_y
            is_m_lc_up = False
        
        if is_key_down:
            event_id += 1
            print('\nIn Loop: key_down:', key_pressed, 'event_id:', event_id)
            bch[event_id] = key_wait_time_ms, 'key_pressed:', key_pressed, None, None
            is_key_down = False
        if is_key_up:
            event_id += 1
            print('\nIn Loop: key_up:', key_pressed, 'event_id:', event_id)
            bch[event_id] = key_wait_time_ms, 'key_pressed:', key_pressed, None, None
            is_key_up = False

except KeyboardInterrupt:
    # end it gracefully
    signal_handler(signal.SIGINT, None)

我尝试使用 replace() 方法,但没有帮助。也许,我可能错过了什么。

python json double-quotes single-quotes
© www.soinside.com 2019 - 2024. All rights reserved.