在Python脚本中集成条或修剪

发布于 2025-01-23 17:55:15 字数 1921 浏览 3 评论 0原文

非常感谢您阅读我的帖子,希望有人可以帮助我,我有一个脚本可以连接到我的数据库,并提取几张表并将其转换为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()

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

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

我想要使用条带或修剪功能来删除空白空间。

您能帮我知道我可以与谁整合吗???

非常感谢。

亲切的问候 !!!

Thanks so much for reading my post, I hope someone can help me with that, I have a script to connect to my database and extract several tables and convert them to JSONL format ( all with pandas ), my script:

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()

My script works fine, the issue I have is having much black spaces in the results, like:

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

I want a use a strip or trim function to delete the blank space.

Can you help me to know who I can integrate that with ???

Thanks so much.

Kind regards !!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

三生池水覆流年 2025-01-30 17:55:15

您应该能够在两条线之间做到这一点:

    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)

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

You should be able to do this right between two of your lines:

    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)

Or wherever you want to do this stripping. Note that the other answer, to operate directly on a dictionary is valid too.

小霸王臭丫头 2025-01-30 17:55:15

我不知道在脚本中添加了Whiteves的位置,但是您可以在之后修剪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}

I don't know where in your script the whitespaces are added, but you can trim the result afterwards.

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}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文