How to replace only certain strings in a text file using csv file

Viewed 191

I have a csv file (script.csv) which is used as a script to replace strings in multiple files. Thease files are graph files. Number of graphs for each file are specified in csv file. For the first file, number of graphs are 1 . For the second file- 3 graphs, For the third file- 2 graphs, For the fourth file-4 graphs, For the 5th file-1 graph, .... likewise.

Data in csv(script.csv)

     Number of Graphs   1       3       2       4       1
         ---            --      --      --      --      --
     y_title           y1       y1      y1      y1      y1
                                y2      y2      y2
                                y3              y3
                                                y4

Suppose x4.txt is a four graph file. then I search for the places of string to replace using regular expression. Then I found five places to replace, but I need to replace only 4 places. For example : if re.search(r'/et y', string): print(string)

 df=pd.read_csv("script.csv")

 class Update:

    ytCount=0

    def file_read(self):

    f1 = open(inpFile, 'r')
    f2 = open(outFile, 'w')
    for line in f1.readlines():
        string=line            
        if re.search(r'/et    y', string):
            self.y_axis_title(string,f2)            
        else:
            f2.write(string)

   def y_axis_title(self,string,f2):
    if nGraphs==1:
        self.ytCount=0
        if self.ytCount == 0:
            word = self.y_change_title(string, ''.join(y_title))
            f2.write(word)
    else:
        for i in range(self.ytCount,nGraphs):
            if self.ytCount==i:
                word=self.y_change_title(string,y_title[i])
                f2.write(word)
        if self.ytCount==nGraphs-1:
                self.ytCount=0
        else:
                self.ytCount+=1
    def y_change_title(self,string,ytitle):
    strplt=string.split()

    word = strplt[0] + "    " + strplt[1] + " " + strplt[2][0] + ytitle + "    "+strplt[2][0]  + "\t" + '   '.join(strplt[4:6]) + "\n"
    return word

 obj=Update()
 colCount=((df.count(axis=1)[0])-1)
 for i in  range(colCount):

nGraphs=int(df.columns[2:][i][0])
inpFile=df[df.columns[2:][i]][0]
outFile=df[df.columns[2:][i]][0]+"_out"    

if nGraphs == 1:       
    y_title = list(df[df.columns[2:][i]][5])    //      y_tiltle in 5th row
elif nGraphs==2:        
    y_title=list(df[df.columns[2:][i]][6:8])  //        y_tiltle in 6 to 7 row

elif nGraphs==3:        
    y_title = list(df[df.columns[2:][i]][7:10])   //        y_tiltle in 7 to 9 row

elif nGraphs == 4:        
    y_title = list(df[df.columns[2:][i]][8:12])     // y_tiltle in 8 to 11 row
obj.file_read()

Input file containing strings to be replaced :

 /et    y "HHJKLJ" ;axis title

/et    y "SDFSDF"  ;axis title

 /et    y "STSTS" ;axis title

 /et    y "uiuhig"   ;axis title

 /et    y "fgfjhgjhg" ;axis title

I want my output as

 /et    y "y1" ;axis title

 /et    y "y2"  ;axis title

 /et    y "y3" ;axis title

 /et    y "y4"   ;axis title

 /et    y "fgfjhgjhg" ;axis title

But what I actually get is:

/et    y "y1" ;axis title

/et    y "y2"  ;axis title

/et    y "y3" ;axis title

/et    y "y4"   ;axis title

/et    y "y1" ;axis title


I dont to replace the fifth string in y1 I want only to replace upto fourth, for the fifth string just write what is in the input file. 

How to fix this?

0 Answers
Related