Python Forum
Replace based on values in a file - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Replace based on values in a file (/thread-39314.html)



Replace based on values in a file - WJSwan - Jan-30-2023

I have a file containing records with 4 values, eg:
1,Genesis,Gen,Gen
2,Eksodus,Eks,Exo
3,Levitikus,Lev,Lev
4,Numeri,Num,Num
5,Deuteronomium,Deut,Deu
6,Josua,Jos,Jos
7,Rigters,Rig,Jdg
8,Rut,Rut,Rth
9,1 Samuel,1 Sam,1Sa

I need to read these into an array and I then need to read a second file and in each record of this file replace occurrences of the 3rd field in the first file (Gen, Eks, Lev, Num, Deut etc.) with the first field (1,2,3, etc. followed by a fullstop, eg 1., 2., 3.).

So if the second has a record reading:

Ps. 19:3; Num 3:1........
This needs to be replaced by:
19.19:3; 4.3:1.....

How would I achieve this? I have just started learning Python, so I am really a novice.


RE: Replace based on values in a file - WJSwan - Jan-30-2023

Here is what I tried, but it does not seem to work.
# Convert
import codecs
import re
def FixXrefs(xref):
      global bookname
      global bookno
      lg.write("----- Entered function\n")
      instuff=xref
      otstuff=""
      n=len(bookname)
      for i in bookno:
          j=int(i)-1
          pattern = bookname[j]+" "
          replaceWith = i+"."
          lg.write("|"+pattern+"|"+replaceWith+"|\n")
          otstuff=instuff.replace(pattern, replaceWith)
      instuff=otstuff.replace(":", ".")
      return instuff
             
lg=open("log.txt", "w")
bookname=[]
bookno=[]
#set up bookname/bookno table
bn=codecs.open("Boeke.txt", "r", "utf-8")
while True:
      l=bn.readline()
      l.strip('\n')
      if ("" == l):
            lg.write("End of bookname/no file reached\n")
            break
      #parse string
      words = l.split(",")
      bookno.append(words[0])
      bookname.append(words[2])
bn.close()
# Now read the file to reformat
fn=codecs.open("Conv.txt", "r", "utf-8")
while True:
      irec=fn.readline()
      pline="INREC\n"+irec
      lg.write(pline)
      if ("" == irec):
        lg.write ("End of Conv file reached\n")
        break;
      nxref = FixXrefs(irec)
      pline="NXREF\n"+nxref
      lg.write(pline)
lg.close()
fn.close()



RE: Replace based on values in a file - deanhystad - Jan-30-2023

I would try re.sub. You might be able to do it with just one command, but "1 Sam" may cayse problems. This could also be done using str.replace(), but matching only complete words might be difficult.

Either way I would treat the file like one long string instead of a line at a time.