问题描述
我正在尝试编写一个 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 (将#修改为@)