Python Forum
Replace based on values in a file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Replace based on values in a file
#2
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()

Attached Files

.txt   Conv.txt (Size: 508 bytes / Downloads: 68)
.txt   Boeke.txt (Size: 1.39 KB / Downloads: 59)
.txt   log.txt (Size: 2.74 KB / Downloads: 59)
Reply


Messages In This Thread
Replace based on values in a file - by WJSwan - Jan-30-2023, 05:45 AM
RE: Replace based on values in a file - by WJSwan - Jan-30-2023, 09:00 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Formatting data based on DataFrames values 577e94982d620b84f7c536d5e76f1e 0 2,070 Dec-03-2018, 02:23 AM
Last Post: 577e94982d620b84f7c536d5e76f1e
  file a table of values Ybivashka322 4 3,751 Dec-14-2017, 06:11 PM
Last Post: mpd
  logic comparater based on dictionary values ijosefson 3 3,269 Oct-16-2017, 06:04 AM
Last Post: Mekire

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020