How to remove spaces while writing in INI file- Python

前端 未结 2 866
-上瘾入骨i
-上瘾入骨i 2021-01-19 02:36

I am using a file and i have one section named DIR in which it contain the paths. EX:

[DIR]
DirTo=D:\\Ashish\\Jab Tak hai Jaan
DirBackup = D:\\Parser\\ERICS         


        
2条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-19 02:59

    I ran into this problem to and I came up with an additional solution.

    • I didn't want to replace the function as future versions of Python might change the internal function structures of RawConfigParser.
    • I also didn't want to read the file back in right after it was written because that seemed wasteful

    Instead I wrote a wrapper around the file object which simply replaces " = " with "=" in all lines written though it.

    class EqualsSpaceRemover:
        output_file = None
        def __init__( self, new_output_file ):
            self.output_file = new_output_file
    
        def write( self, what ):
            self.output_file.write( what.replace( " = ", "=", 1 ) )
    
    config.write( EqualsSpaceRemover( cfgfile ) )
    

提交回复
热议问题