search and replace first amount of strings instances with one thing and a second amou

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chickflick91
    New Member
    • Sep 2017
    • 8

    search and replace first amount of strings instances with one thing and a second amou

    i have a code in python to search and replace what i need though is to replace the first say 10 instances of the number 1 with 2 and the second 10 instances with the number 3. anybody knows how to do that?


    fin = open(r'F:\1\xxx .txt')
    fout = open(r'F:\1\xxx 2.txt', "wt")
    for line in fin:
    fout.write( line.replace('1 ', '2') )
    fin.close()
    fout.close()
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    Use a counter to count the number of replaces
    Code:
    num_replaced=0
    fin = open(r'F:\1\xxx.txt')
    fout = open(r'F:\1\xxx2.txt', "wt")
    replace_out="2"
    ctr=0
    for line in fin:
        fout.write( line.replace('1', replace_out) )
        ctr += 1
        if ctr > 9:
            replace_out="3"
            ctr=0
    fin.close()
    fout.close()

    Comment

    • chickflick91
      New Member
      • Sep 2017
      • 8

      #3
      didnt work for some reason. i tested putting in xxx.txt
      1111111111
      1111111111
      and i got
      2222222222
      2222222222

      in xxx2.txt

      Comment

      • chickflick91
        New Member
        • Sep 2017
        • 8

        #4
        so i was given the code

        with open(r'F:\1\xxx .txt') as fin:
        lines = fin.read()
        # get a list of all instances of number 1
        all_index = [i for i,x in enumerate(lines ) if i != 0 and lines[i-1:i+1] == 'x1']
        # convert the file string into a list
        list_lines = list(lines)
        # replace the first 10 instances with 2;
        # then the second 10 instances with 3
        for i in all_index[:10]:
        list_lines[i] = '2'
        for i in all_index[10:20]:
        list_lines[i] = '3'
        # write the resulting string into output file
        with open(r'F:\1\xxx 2.txt','w') as fout:
        fout.write(''.j oin(list_lines) )




        and it works in replacing the string 'x1'. which almost gets me exactly what i need but doesn't work with longer strings like 'stop1'. does anybody know why?

        Comment

        • dwblas
          Recognized Expert Contributor
          • May 2008
          • 626

          #5
          I have no idea what you want to do, and "didn't work" provides no additional information.
          didnt work for some reason. i tested putting in xxx.txt
          1111111111
          1111111111
          and i got
          2222222222
          2222222222
          when you said
          replace the first say 10 instances of the number 1 with 2
          Isn't that what it just did on each of these lines???

          Comment

          • chickflick91
            New Member
            • Sep 2017
            • 8

            #6
            solved by code

            get_all_index = lambda s : [i for i,_ in enumerate(lines ) if i-len(s)+1 >= 0 and lines[i-len(s)+1:i+1] == s]


            with open(r'F:\1\xxx .txt') as fin:
            lines = fin.read()
            # get a list of all instances of number 1
            all_index = get_all_index(' yyyy1')
            # convert the file string into a list
            list_lines = list(lines)
            # replace the first 10 instances with 2;
            # then the second 10 instances with 3
            for i in all_index[:10]:
            list_lines[i] = '2'
            for i in all_index[10:20]:
            list_lines[i] = '3'
            # write the resulting string into output file
            with open(r'F:\1\xxx 2.txt','w') as fout:
            fout.write(''.j oin(list_lines) )

            Comment

            Working...