在 Python 中创建漂亮的格式

问题描述

我正在尝试编写一个 python 脚本来帮助我格式化 sql。目标是让数据格式和NULL 的行整齐。

看起来像这样:

CREATE TABLE [dbo].[?????????????????????????](,[timestamp] TIMESTAMP NULL,[??????] INT NULL,[?????????] NVARCHAR(20) NULL

我希望它看起来像这样:

CREATE TABLE [dbo].[?????????????????????????](,[timestamp]      TIMESTAMP    NULL,[??????]         INT          NULL,[?????????]      NVARCHAR(20) NULL

我编写了用于排列数据类型的代码这有效!

maxh = 0

fhand = open('file name')
for line in fhand:
    line = line.rstrip()
    if line.startswith(',['):                #find lines that need formating
        hindex = line.index(']')               #find the position of ]
        if hindex > maxh: maxh = hindex        #find the range that needs to be covered
        x = maxh - hindex                      #find the number of spaces needed 
        print(line.replace(']',(']'+(' '*x)))) #print the right amount of spaces
    else:
        print(line)                            #print all lines that do not need formating

我的代码排空 NULL 不起作用当我运行它时,输出文件相同。

maxnull = 0

fhand = open('filename')
for line in fhand:
    line = line.rstrip()
    if line.startswith(',['):
        nullindex = line.find('NULL')
        if nullindex > maxnull: maxnull = nullindex
        y = maxnull - nullindex
        print(line.replace('NULL',('NULL'+(' '*y))))
    else:
        print(line)

我不明白为什么第一部分有效,而第二部分却没有。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...