在 python 脚本中集成 strip 或 trim

Integrate strip or trim in python script

非常感谢阅读我的 post,我希望有人能帮助我,我有一个脚本可以连接到我的数据库并提取几个表并将它们转换为 JSONL 格式(全部带有 pandas ), 我的脚本:

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=12345;'
                      '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_o = df.astype(str)
    df_o.to_json(filename_json, orient = "records",  lines = bool, date_format = "iso", 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()

我的脚本工作正常,我遇到的问题是结果中有很多黑色 spaces,例如:

{"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}

我想使用 strip 或 trim 函数删除空白 space。

你能帮我知道我可以和谁整合吗???

非常感谢。

亲切问候!!!

我不知道在你的脚本中哪里添加了空格,但你可以 trim 然后 result

result = {k: v.rstrip() if isinstance(v, str) else v for k, v in result.items()}
>>> result
{'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}

您应该能够在两行之间执行此操作:

    df_o = df.astype(str)
    df_o = df_o.applymap(lambda x: x.strip() if isinstance(x, str) else x)
    df_o.to_json(filename_json, orient = "records",  lines = bool, date_format = "iso", double_precision = 15, force_ascii = False, date_unit = 'ms', default_handler = str)

或者在任何你想进行剥离的地方。请注意,另一个答案,直接在字典上操作也是有效的。