如何解决在 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 举报,一经查实,本站将立刻删除。