替换字符串中的特定控制/不可打印字符

时间:2019-05-19 17:48:59

标签: python python-3.x python-3.7 cx-oracle

我正在编写一个读取sql文件并使用cx_Oracle执行文件的脚本。有些文件仅在每个sql运算符和关键字之间使用空格写入,而其他文件则使用换行符和制表符作为空白。我面临后者的问题。例如,本节返回以下内容:

NON_PRINTABLE = """\r\n\t"""
def parseSQLFile(filename):
    with open(filename, 'r') as sqlFile:
        allSQL = sqlFile.read()
        #filteredSQL = filter(lambda ln: ln in string.printable, allSQL)
        # replace specific control characters with spaces to prevent sql compiler errors
        for char in NON_PRINTABLE:
            allSQL.replace(char,' ')


    return allSQL

我尝试使用过滤器功能,翻译和替换;但是,我仍然可以从以下输入中得到以下不同结果:

输入:

'select \ n \ ts.id \ n \ t,s.src_cnt \ n \ t,s.out_file \ t \ nfrom \ n \ tkpi_index_ros.composites s \ n \ t,kpi_index_ros.kpi_index_rosoards d \ nwhere \ n \ t1 = 1 \ n \ t s.kpi_index_rosoard_id(+)= d.id \ n \ t和d.active = 1 \ n;'

输出1:

'select \ n s.id \ n,s.src_cnt \ n,s.out_file \ nfrom \ n kpi_index_ros.composites s \ n,kpi_index_ros.kpi_index_rosoards d \ n其中\ n 1 = 1 \ n和s.kpi_index_rosoard_id (+)= d.id \ n和d.active = 1 \ n;'

输出2:

'从\ tkpi_index_ros.composites s选择\ ts.id \ t,s.src_cnt \ t,s.out_file \ t \ t,kpi_index_ros.kpi_index_rosoards d其中\ t1 = 1 \ t和s.kpi_index_rosoard_id(+)= d.id \ tand d.active = 1;'

似乎它将替换选项卡或换行符,但不会同时替换两者。有什么办法可以有效地做到这一点?

1 个答案:

答案 0 :(得分:1)

进行以下更改(用String对象的.replace方法的输出替换allSQL值)会产生所需的输出:

NON_PRINTABLE = """\r\n\t"""
def parseSQLFile(filename):
    with open(filename, 'r') as sqlFile:
        allSQL = sqlFile.read()
        # replace specific control characters with spaces to prevent sql compiler errors
        for char in NON_PRINTABLE:
            allSQL = allSQL.replace(char,' ') #updating allSQL with returned value

    return allSQL

输出:

'select  s.id  ,s.src_cnt  ,s.out_file  from  kpi_index_ros.composites s  ,kpi_index_ros.kpi_index_rosoards d where  1 = 1  and s.kpi_index_rosoard_id (+) = d.id  and d.active = 1 ;'

问题的第二部分-关于这种方法的效率,您可能应该参考benchmark results in this answer