Hamburger Hamburger

python Library

replaceInFile

Keine Erläuterungen gefunden.

import  os, sys, argparse

#=======================================#
def  reformatAuthor(line):
    """
    search and replace string
    """
    if line.find("UTHOR =")>0:
        line = line.replace("/", "and")

    return line

#=======================================#
def  commandLineArgs():
    """
    Parse command line arguments,
    call: python replaceInFile.py -f myFile.txt -n 69
    """
    parser = argparse.ArgumentParser(description="Do some string replacements in file.", epilog = "call: python replaceInFile.py -f myFile.txt -n 69")
    parser.add_argument("-f", "--file", help = "Enter file to process")
    parser.add_argument("-n", "--No",   help = "Enter some number", required = False, default = "3.14", type=float)
    args = parser.parse_args()
    inFile = args.file
    
    #optional defaults:
    if not len(inFile)>1:
        inFile  = "bisam.bib"
        
    return inFile

#=======================================#
def  processFile(inFile):
    """
    For each line of the input file,
    do some string replacements.
    """
    if os.path.isfile(inFile):
        outFile = inFile[:inFile.refind(".")]+".out"
        ifp = open(inFile, 'r', encoding='utf-8')
        ofp = open(outFile, 'w', encoding='utf-8')

        line = ifp.readline()
        while line:
            line = reformatAuthor(line)
            ofp.write(line)
            line = ifp.readline()

        ifp.close()
        ofp.close()
    else:
        print("File does not exist!")
        print(inFile)
    
#=======================================#
if __name__ == "__main__":
    inFile = commandLineArgs()
    processFile(inFile)

Index of Library

1ArnesKonto.py
2CIEcolorCoordinates_Spectrum.py
3MatlabStructures.py
4ModelicaExecution.py
5ModelicaMatFilter.py
6OperateAllFiles.py
7dictionaryStatistics.py
8familienKonto.py
9makeDoc.py
10plotTimeSeries.py
11readData2DictOfArrays.py
12replaceInFile.py
13showPointOnCIExy.py
14svg2pdf.py
15testNumpyMatrix.py
16writeDictOfArrays.py
17writeTSV.py

Der gesamte Sourcecode darf gemäß GNU General Public License weiterverbreitet werden.