结果中的引用问题 ( python )

Quotation problems in results ( python )

我希望你能帮我解决这些问题,我创建了一个脚本来从数据库中提取一些表,然后将它们转换为 JSONL 文件。

我的脚本有效,但当涉及到结果 (JSONL) 时,一些引号 " 从结果中删除,我不知道为什么,我的脚本:

import pyodbc
import fileinput
import csv
import pandas as pd
import json
import os
import sys

conn = pyodbc.connect('Driver={SQL Server};'
                      'Server=Test;'
                      'UID=test;'
                      'PWD=test;'
                      'Database=TEST;'
                      'Trusted_Connection=no;')
cursor = conn.cursor()

query = "SELECT * FROM placeholder"


with open(r"D:\Test.txt") as file:
    lines = file.readlines()
    print(lines)


for user_input in lines:

    result = query.replace("placeholder", user_input)
    print(result)
    sql_query = pd.read_sql(result,conn)
    df = pd.DataFrame(sql_query)
    user_inputs =  user_input.strip("\n")
    filename = os.path.join('D:\', user_inputs + '.csv')
    df.to_csv (filename, index = False, encoding='utf-8', sep = '~', quotechar = "`", quoting=csv.QUOTE_ALL)
    print(filename)
    filename_json = os.path.join('D:\', user_inputs + '.jsonl')
    csvFilePath = (filename)
    jsonFilePath = (filename_json)
    print(filename_json)
    df.to_json(filename_json, orient = "records",  lines = bool, date_format = "epoch", double_precision = 15, force_ascii = False, date_unit = 'ms', default_handler = str)

dir_name = "D:\"
test = os.listdir(dir_name)

for item in test:
    if item.endswith(".csv"):
        os.remove(os.path.join(dir_name, item))

cursor.close()
conn.close()

现在的结果(示例):

{"SucCod":1,"SucNom":"CENTRAL                  ","SucUsrMod":"aleos     ","SucFecMod":1537920000000,"SucHorMod":"11:30:21","SucTip":"S","SucBocFac":4,"SucCal":"SUTH               ","SucNro":1524,"SucPis":6,"SucDto":"    ","SucCarTel":"55   ","SucTel":52001}

我看(例子):

{"SucCod":"1","SucNom":"CENTRAL                  ","SucUsrMod":"aleos     ","SucFecMod":"1537920000000","SucHorMod":"11:30:21","SucTip":"S","SucBocFac":"4","SucCal":"SUTH               ","SucNro":"1524","SucPis":"6","SucDto":"    ","SucCarTel":"55   ","SucTel":"52001"}

现在您可以看到一些数据缺少引号“

检查 CSV 文件后,他们保留了引号,因此问题出在转换为 JSONL 时。

你知道我需要用来维护报价单的开关吗???

非常感谢。

亲切的问候。

如果您的 DataFrame 中的所有内容都必须是字符串,请尝试使用 DataFrame.astype

df_o = df.astype(str)
df_o.to_json()

否则:

df_o = df.astype(object)
df_o.to_json()