如何清除原始 sql 文件中的注释
我在清理现有 sql 文件中的注释和空行时遇到问题。 该文件有超过 10k 行,因此无法手动清理它。
我有一个小 python 脚本,但我不知道如何处理多行插入中的注释。
代码:
f = file( 'file.sql', 'r' )
t = filter( lambda x: not x.startswith('--') \
and not x.isspace()
, f.readlines() )
f.close()
t #<- here the cleaned data should be
它应该如何工作:
这应该被清理:
-- normal sql comment
这应该保持原样:
CREATE FUNCTION func1(a integer) RETURNS void
LANGUAGE plpgsql
AS $$
BEGIN
-- comment
[...]
END;
$$;
INSERT INTO public.texts (multilinetext) VALUES ('
and more lines here \'
-- part of text
\'
[...]
');
I have problem with cleaning comments and empty lines from already existing sql file.
The file has over 10k lines so cleaning it manually is not an option.
I have a little python script, but I have no idea how to handle comments inside multi line inserts.
Code:
f = file( 'file.sql', 'r' )
t = filter( lambda x: not x.startswith('--') \
and not x.isspace()
, f.readlines() )
f.close()
t #<- here the cleaned data should be
How it should work:
This should be cleaned:
-- normal sql comment
This should stay as it is:
CREATE FUNCTION func1(a integer) RETURNS void
LANGUAGE plpgsql
AS $
BEGIN
-- comment
[...]
END;
$;
INSERT INTO public.texts (multilinetext) VALUES ('
and more lines here \'
-- part of text
\'
[...]
');
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
正则表达式字符串解释:
有关更多详细信息,请参阅 Python 正则表达式文档:
https://docs.python.org/3/library/re.html
Regex string explained:
See the Python regular expressions documentation for more details:
https://docs.python.org/3/library/re.html
可以使用正则表达式来做到这一点。首先,您必须按字符串拆分文件,然后您可以按注释拆分文件。下面的 Perl 程序可以做到这一点:
It is possible to do it with regular expressions. First you have to split the file by strings and after this you can split the file by comments. The following Perl program does it:
这是示例偏差答案的扩展,适用于您的示例:
输出:
This is an extend of samplebias answer that work with your example :
Output:
尝试使用 sqlparse 模块。
更新的示例:在插入值内留下注释,并在 CREATE FUNCTION 块内留下注释。您可以进一步调整以调整行为:
输出:
Try the sqlparse module.
Updated example: leaving comments inside insert values, and comments within CREATE FUNCTION blocks. You can tweak further to tune the behavior:
Output:
添加更新的答案:)
输出:
它实现了相同的结果,但也涵盖了所有其他极端情况并且更简洁
Adding an updated answer :)
Output:
It achieves the same result but also covers all other corner cases and more concise