Dr. Arne JachensDr. Arne Jachens

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

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

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