Python text processing





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I have a data file with huge data in it. I am only interested in two codes per user that needs to be updated. I have the new codes in separate file. I just want to compare both files and add new codes to existing files.



Old File: (txt2)



.
..
..
alpha Donec vulputate lorem tortor, nec fermentum nibh bibendum vel.
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Praesent dictum luctus massa, non euismod lacus.
${alpha_john}: 'Lorem ipsum dolor sit amet, consectetur'
${beta_john}: 'iuhertgh jndsfbjpijwrg'
${alpha_mac}: 'acerat a lorem eget, ultricies'
${beta_mac}: 'elit nibh, eu condimentum orci viverra q'
${alpha_joe}: 'gravida lorem, ut congue diam.'
${beta_joe}: 'orttitor in condimentum nec, venenatis eu urna'
${alpha_mark}: ''
${beta_mark}: ''
${alpha_ross}: 'suscipit vitae felis non suscipit.'
${beta_ross}: 'non vulputate convallis, ligula diam sagittis urna, in venenatis'
${alpha_don}: 'Pellentesque feugiat diam est, at rhoncus orci porttitor'
${beta_don}: 'Sed elementum elit nibh'
${alpha_harry}: 'Proin tempor lacus arcu.'
${beta_harry}: 'posuere sollicitudin mi, et vulputate nisl fringilla non'
Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.
Aliquam euismod ultrices lorem, sit amet imperdiet est tincidunt vel.
Phasellus dictum justo sit amet ligula varius aliquet auctor et metus.
..
..
.


Codes file: (txt1)



${alpha_john}: 'XXXXXHHHHHHHXXXXXX'
${beta_john}: 'XFFFFFFFFFGGGGGGGGDDDDDD'
${alpha_mac}: 'DDDDDDKKKKKKKKK'
${beta_mac}: 'KKKKKKKKKKKYYYYYYYYYYYYD'
${alpha_joe}: 'TTTTTVVVVVVVVVVVKK'
${beta_joe}: 'OOOOOOOSSSSSSSSSSPPPPPP'
${alpha_ross}: 'SSSSSHHHHHHHHTTTTTTTT'
${beta_ross}: 'PPPPPWWWWWHHHHHHHHHH'
${alpha_harry}: 'IIIIIIEEEEEEETTTTTTTTTT'
${beta_harry}: 'YYYYYYYYEEEEEEEEEEMMMMMMMMMM'


My Code:



#!/usr/bin/env python

import os, sys, re, time
import argparse
import logging
import time

cat /dev/null > /home/user/scripts/temp/txt3

file1=open("/home/user/scripts/temp/txt1",'r+')
file2=open("/home/user/scripts/temp/txt2", 'r+')
file3=open("/home/user/scripts/temp/txt3", 'r+')

for line1 in file1:
keyword=line1[line1.find("{")+1:line1.find("}")]
for line2 in file2:
if keyword in line2:
file3.write(line1)
else:
file3.write(line2)
file1.close()
file2.close()
file3.close()


Output:



alpha Donec vulputate lorem tortor, nec fermentum nibh bibendum vel.
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Praesent dictum luctus massa, non euismod lacus.
${alpha_john}: 'XXXXXHHHHHHHXXXXXX'
${beta_john}: 'iuhertgh jndsfbjpijwrg'
${alpha_mac}: 'acerat a lorem eget, ultricies'
${beta_mac}: 'elit nibh, eu condimentum orci viverra q'
${alpha_joe}: 'gravida lorem, ut congue diam.'
${beta_joe}: 'orttitor in condimentum nec, venenatis eu urna'
${alpha_mark}: ''
${beta_mark}: ''
${alpha_ross}: 'suscipit vitae felis non suscipit.'
${beta_ross}: 'non vulputate convallis, ligula diam sagittis urna, in venenatis'
${alpha_don}: 'Pellentesque feugiat diam est, at rhoncus orci porttitor'
${beta_don}: 'Sed elementum elit nibh'
${alpha_harry}: 'Proin tempor lacus arcu.'
${beta_harry}: 'posuere sollicitudin mi, et vulputate nisl fringilla non'
Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.
Aliquam euismod ultrices lorem, sit amet imperdiet est tincidunt vel.
Phasellus dictum justo sit amet ligula varius aliquet auctor et metus.


this code just prints one line from txt1 '${alpha_john}: 'XXXXXHHHHHHHXXXXXX'' in new file but rest of the lines stays as those are in old file (txt2).



What to do so all lines from (txt1) get overwritten?



Let me know for any additional infromation required.










share|improve this question































    0















    I have a data file with huge data in it. I am only interested in two codes per user that needs to be updated. I have the new codes in separate file. I just want to compare both files and add new codes to existing files.



    Old File: (txt2)



    .
    ..
    ..
    alpha Donec vulputate lorem tortor, nec fermentum nibh bibendum vel.
    Lorem ipsum dolor sit amet, consectetur adipiscing elit.
    Praesent dictum luctus massa, non euismod lacus.
    ${alpha_john}: 'Lorem ipsum dolor sit amet, consectetur'
    ${beta_john}: 'iuhertgh jndsfbjpijwrg'
    ${alpha_mac}: 'acerat a lorem eget, ultricies'
    ${beta_mac}: 'elit nibh, eu condimentum orci viverra q'
    ${alpha_joe}: 'gravida lorem, ut congue diam.'
    ${beta_joe}: 'orttitor in condimentum nec, venenatis eu urna'
    ${alpha_mark}: ''
    ${beta_mark}: ''
    ${alpha_ross}: 'suscipit vitae felis non suscipit.'
    ${beta_ross}: 'non vulputate convallis, ligula diam sagittis urna, in venenatis'
    ${alpha_don}: 'Pellentesque feugiat diam est, at rhoncus orci porttitor'
    ${beta_don}: 'Sed elementum elit nibh'
    ${alpha_harry}: 'Proin tempor lacus arcu.'
    ${beta_harry}: 'posuere sollicitudin mi, et vulputate nisl fringilla non'
    Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.
    Aliquam euismod ultrices lorem, sit amet imperdiet est tincidunt vel.
    Phasellus dictum justo sit amet ligula varius aliquet auctor et metus.
    ..
    ..
    .


    Codes file: (txt1)



    ${alpha_john}: 'XXXXXHHHHHHHXXXXXX'
    ${beta_john}: 'XFFFFFFFFFGGGGGGGGDDDDDD'
    ${alpha_mac}: 'DDDDDDKKKKKKKKK'
    ${beta_mac}: 'KKKKKKKKKKKYYYYYYYYYYYYD'
    ${alpha_joe}: 'TTTTTVVVVVVVVVVVKK'
    ${beta_joe}: 'OOOOOOOSSSSSSSSSSPPPPPP'
    ${alpha_ross}: 'SSSSSHHHHHHHHTTTTTTTT'
    ${beta_ross}: 'PPPPPWWWWWHHHHHHHHHH'
    ${alpha_harry}: 'IIIIIIEEEEEEETTTTTTTTTT'
    ${beta_harry}: 'YYYYYYYYEEEEEEEEEEMMMMMMMMMM'


    My Code:



    #!/usr/bin/env python

    import os, sys, re, time
    import argparse
    import logging
    import time

    cat /dev/null > /home/user/scripts/temp/txt3

    file1=open("/home/user/scripts/temp/txt1",'r+')
    file2=open("/home/user/scripts/temp/txt2", 'r+')
    file3=open("/home/user/scripts/temp/txt3", 'r+')

    for line1 in file1:
    keyword=line1[line1.find("{")+1:line1.find("}")]
    for line2 in file2:
    if keyword in line2:
    file3.write(line1)
    else:
    file3.write(line2)
    file1.close()
    file2.close()
    file3.close()


    Output:



    alpha Donec vulputate lorem tortor, nec fermentum nibh bibendum vel.
    Lorem ipsum dolor sit amet, consectetur adipiscing elit.
    Praesent dictum luctus massa, non euismod lacus.
    ${alpha_john}: 'XXXXXHHHHHHHXXXXXX'
    ${beta_john}: 'iuhertgh jndsfbjpijwrg'
    ${alpha_mac}: 'acerat a lorem eget, ultricies'
    ${beta_mac}: 'elit nibh, eu condimentum orci viverra q'
    ${alpha_joe}: 'gravida lorem, ut congue diam.'
    ${beta_joe}: 'orttitor in condimentum nec, venenatis eu urna'
    ${alpha_mark}: ''
    ${beta_mark}: ''
    ${alpha_ross}: 'suscipit vitae felis non suscipit.'
    ${beta_ross}: 'non vulputate convallis, ligula diam sagittis urna, in venenatis'
    ${alpha_don}: 'Pellentesque feugiat diam est, at rhoncus orci porttitor'
    ${beta_don}: 'Sed elementum elit nibh'
    ${alpha_harry}: 'Proin tempor lacus arcu.'
    ${beta_harry}: 'posuere sollicitudin mi, et vulputate nisl fringilla non'
    Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.
    Aliquam euismod ultrices lorem, sit amet imperdiet est tincidunt vel.
    Phasellus dictum justo sit amet ligula varius aliquet auctor et metus.


    this code just prints one line from txt1 '${alpha_john}: 'XXXXXHHHHHHHXXXXXX'' in new file but rest of the lines stays as those are in old file (txt2).



    What to do so all lines from (txt1) get overwritten?



    Let me know for any additional infromation required.










    share|improve this question



























      0












      0








      0


      2






      I have a data file with huge data in it. I am only interested in two codes per user that needs to be updated. I have the new codes in separate file. I just want to compare both files and add new codes to existing files.



      Old File: (txt2)



      .
      ..
      ..
      alpha Donec vulputate lorem tortor, nec fermentum nibh bibendum vel.
      Lorem ipsum dolor sit amet, consectetur adipiscing elit.
      Praesent dictum luctus massa, non euismod lacus.
      ${alpha_john}: 'Lorem ipsum dolor sit amet, consectetur'
      ${beta_john}: 'iuhertgh jndsfbjpijwrg'
      ${alpha_mac}: 'acerat a lorem eget, ultricies'
      ${beta_mac}: 'elit nibh, eu condimentum orci viverra q'
      ${alpha_joe}: 'gravida lorem, ut congue diam.'
      ${beta_joe}: 'orttitor in condimentum nec, venenatis eu urna'
      ${alpha_mark}: ''
      ${beta_mark}: ''
      ${alpha_ross}: 'suscipit vitae felis non suscipit.'
      ${beta_ross}: 'non vulputate convallis, ligula diam sagittis urna, in venenatis'
      ${alpha_don}: 'Pellentesque feugiat diam est, at rhoncus orci porttitor'
      ${beta_don}: 'Sed elementum elit nibh'
      ${alpha_harry}: 'Proin tempor lacus arcu.'
      ${beta_harry}: 'posuere sollicitudin mi, et vulputate nisl fringilla non'
      Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.
      Aliquam euismod ultrices lorem, sit amet imperdiet est tincidunt vel.
      Phasellus dictum justo sit amet ligula varius aliquet auctor et metus.
      ..
      ..
      .


      Codes file: (txt1)



      ${alpha_john}: 'XXXXXHHHHHHHXXXXXX'
      ${beta_john}: 'XFFFFFFFFFGGGGGGGGDDDDDD'
      ${alpha_mac}: 'DDDDDDKKKKKKKKK'
      ${beta_mac}: 'KKKKKKKKKKKYYYYYYYYYYYYD'
      ${alpha_joe}: 'TTTTTVVVVVVVVVVVKK'
      ${beta_joe}: 'OOOOOOOSSSSSSSSSSPPPPPP'
      ${alpha_ross}: 'SSSSSHHHHHHHHTTTTTTTT'
      ${beta_ross}: 'PPPPPWWWWWHHHHHHHHHH'
      ${alpha_harry}: 'IIIIIIEEEEEEETTTTTTTTTT'
      ${beta_harry}: 'YYYYYYYYEEEEEEEEEEMMMMMMMMMM'


      My Code:



      #!/usr/bin/env python

      import os, sys, re, time
      import argparse
      import logging
      import time

      cat /dev/null > /home/user/scripts/temp/txt3

      file1=open("/home/user/scripts/temp/txt1",'r+')
      file2=open("/home/user/scripts/temp/txt2", 'r+')
      file3=open("/home/user/scripts/temp/txt3", 'r+')

      for line1 in file1:
      keyword=line1[line1.find("{")+1:line1.find("}")]
      for line2 in file2:
      if keyword in line2:
      file3.write(line1)
      else:
      file3.write(line2)
      file1.close()
      file2.close()
      file3.close()


      Output:



      alpha Donec vulputate lorem tortor, nec fermentum nibh bibendum vel.
      Lorem ipsum dolor sit amet, consectetur adipiscing elit.
      Praesent dictum luctus massa, non euismod lacus.
      ${alpha_john}: 'XXXXXHHHHHHHXXXXXX'
      ${beta_john}: 'iuhertgh jndsfbjpijwrg'
      ${alpha_mac}: 'acerat a lorem eget, ultricies'
      ${beta_mac}: 'elit nibh, eu condimentum orci viverra q'
      ${alpha_joe}: 'gravida lorem, ut congue diam.'
      ${beta_joe}: 'orttitor in condimentum nec, venenatis eu urna'
      ${alpha_mark}: ''
      ${beta_mark}: ''
      ${alpha_ross}: 'suscipit vitae felis non suscipit.'
      ${beta_ross}: 'non vulputate convallis, ligula diam sagittis urna, in venenatis'
      ${alpha_don}: 'Pellentesque feugiat diam est, at rhoncus orci porttitor'
      ${beta_don}: 'Sed elementum elit nibh'
      ${alpha_harry}: 'Proin tempor lacus arcu.'
      ${beta_harry}: 'posuere sollicitudin mi, et vulputate nisl fringilla non'
      Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.
      Aliquam euismod ultrices lorem, sit amet imperdiet est tincidunt vel.
      Phasellus dictum justo sit amet ligula varius aliquet auctor et metus.


      this code just prints one line from txt1 '${alpha_john}: 'XXXXXHHHHHHHXXXXXX'' in new file but rest of the lines stays as those are in old file (txt2).



      What to do so all lines from (txt1) get overwritten?



      Let me know for any additional infromation required.










      share|improve this question
















      I have a data file with huge data in it. I am only interested in two codes per user that needs to be updated. I have the new codes in separate file. I just want to compare both files and add new codes to existing files.



      Old File: (txt2)



      .
      ..
      ..
      alpha Donec vulputate lorem tortor, nec fermentum nibh bibendum vel.
      Lorem ipsum dolor sit amet, consectetur adipiscing elit.
      Praesent dictum luctus massa, non euismod lacus.
      ${alpha_john}: 'Lorem ipsum dolor sit amet, consectetur'
      ${beta_john}: 'iuhertgh jndsfbjpijwrg'
      ${alpha_mac}: 'acerat a lorem eget, ultricies'
      ${beta_mac}: 'elit nibh, eu condimentum orci viverra q'
      ${alpha_joe}: 'gravida lorem, ut congue diam.'
      ${beta_joe}: 'orttitor in condimentum nec, venenatis eu urna'
      ${alpha_mark}: ''
      ${beta_mark}: ''
      ${alpha_ross}: 'suscipit vitae felis non suscipit.'
      ${beta_ross}: 'non vulputate convallis, ligula diam sagittis urna, in venenatis'
      ${alpha_don}: 'Pellentesque feugiat diam est, at rhoncus orci porttitor'
      ${beta_don}: 'Sed elementum elit nibh'
      ${alpha_harry}: 'Proin tempor lacus arcu.'
      ${beta_harry}: 'posuere sollicitudin mi, et vulputate nisl fringilla non'
      Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.
      Aliquam euismod ultrices lorem, sit amet imperdiet est tincidunt vel.
      Phasellus dictum justo sit amet ligula varius aliquet auctor et metus.
      ..
      ..
      .


      Codes file: (txt1)



      ${alpha_john}: 'XXXXXHHHHHHHXXXXXX'
      ${beta_john}: 'XFFFFFFFFFGGGGGGGGDDDDDD'
      ${alpha_mac}: 'DDDDDDKKKKKKKKK'
      ${beta_mac}: 'KKKKKKKKKKKYYYYYYYYYYYYD'
      ${alpha_joe}: 'TTTTTVVVVVVVVVVVKK'
      ${beta_joe}: 'OOOOOOOSSSSSSSSSSPPPPPP'
      ${alpha_ross}: 'SSSSSHHHHHHHHTTTTTTTT'
      ${beta_ross}: 'PPPPPWWWWWHHHHHHHHHH'
      ${alpha_harry}: 'IIIIIIEEEEEEETTTTTTTTTT'
      ${beta_harry}: 'YYYYYYYYEEEEEEEEEEMMMMMMMMMM'


      My Code:



      #!/usr/bin/env python

      import os, sys, re, time
      import argparse
      import logging
      import time

      cat /dev/null > /home/user/scripts/temp/txt3

      file1=open("/home/user/scripts/temp/txt1",'r+')
      file2=open("/home/user/scripts/temp/txt2", 'r+')
      file3=open("/home/user/scripts/temp/txt3", 'r+')

      for line1 in file1:
      keyword=line1[line1.find("{")+1:line1.find("}")]
      for line2 in file2:
      if keyword in line2:
      file3.write(line1)
      else:
      file3.write(line2)
      file1.close()
      file2.close()
      file3.close()


      Output:



      alpha Donec vulputate lorem tortor, nec fermentum nibh bibendum vel.
      Lorem ipsum dolor sit amet, consectetur adipiscing elit.
      Praesent dictum luctus massa, non euismod lacus.
      ${alpha_john}: 'XXXXXHHHHHHHXXXXXX'
      ${beta_john}: 'iuhertgh jndsfbjpijwrg'
      ${alpha_mac}: 'acerat a lorem eget, ultricies'
      ${beta_mac}: 'elit nibh, eu condimentum orci viverra q'
      ${alpha_joe}: 'gravida lorem, ut congue diam.'
      ${beta_joe}: 'orttitor in condimentum nec, venenatis eu urna'
      ${alpha_mark}: ''
      ${beta_mark}: ''
      ${alpha_ross}: 'suscipit vitae felis non suscipit.'
      ${beta_ross}: 'non vulputate convallis, ligula diam sagittis urna, in venenatis'
      ${alpha_don}: 'Pellentesque feugiat diam est, at rhoncus orci porttitor'
      ${beta_don}: 'Sed elementum elit nibh'
      ${alpha_harry}: 'Proin tempor lacus arcu.'
      ${beta_harry}: 'posuere sollicitudin mi, et vulputate nisl fringilla non'
      Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.
      Aliquam euismod ultrices lorem, sit amet imperdiet est tincidunt vel.
      Phasellus dictum justo sit amet ligula varius aliquet auctor et metus.


      this code just prints one line from txt1 '${alpha_john}: 'XXXXXHHHHHHHXXXXXX'' in new file but rest of the lines stays as those are in old file (txt2).



      What to do so all lines from (txt1) get overwritten?



      Let me know for any additional infromation required.







      python text-processing python-textprocessing






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 23 at 11:26









      anatol

      799916




      799916










      asked Jan 3 at 10:56









      data-bitedata-bite

      1019




      1019
























          1 Answer
          1






          active

          oldest

          votes


















          2














          You're iterating through file2 len(file1) times which is certainly not what you want to do. You want to construct a replacement dictionary from file1 like so:



          import re

          # regex to find usernames.
          # You can use str.split to find the usernames like you did if you're
          # not comfortable with regular expressions.
          user_regex = re.compile(r'^${([a-zA-Z0-9_]+)}: ')

          # rename files to something better
          codes_file = "/home/user/scripts/temp/txt1"
          old_file = "/home/user/scripts/temp/txt2"
          new_file = "/home/user/scripts/temp/txt3"

          codes = {}
          with open(codes) as f: # use with to safely open files
          for line in f:
          match = user_regex.search(line)
          if match:
          codes[match.group(1)] = line

          # now we have the codes in ram for easy lookup

          with open(old_file) as old, open(new_file, 'w') as new:
          for line in old:
          match = user_regex.search(line)
          if match and match.group(1) in codes.keys():
          new.write(codes[match.group(1)])
          else:
          new.write(line)





          share|improve this answer


























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54020946%2fpython-text-processing%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            2














            You're iterating through file2 len(file1) times which is certainly not what you want to do. You want to construct a replacement dictionary from file1 like so:



            import re

            # regex to find usernames.
            # You can use str.split to find the usernames like you did if you're
            # not comfortable with regular expressions.
            user_regex = re.compile(r'^${([a-zA-Z0-9_]+)}: ')

            # rename files to something better
            codes_file = "/home/user/scripts/temp/txt1"
            old_file = "/home/user/scripts/temp/txt2"
            new_file = "/home/user/scripts/temp/txt3"

            codes = {}
            with open(codes) as f: # use with to safely open files
            for line in f:
            match = user_regex.search(line)
            if match:
            codes[match.group(1)] = line

            # now we have the codes in ram for easy lookup

            with open(old_file) as old, open(new_file, 'w') as new:
            for line in old:
            match = user_regex.search(line)
            if match and match.group(1) in codes.keys():
            new.write(codes[match.group(1)])
            else:
            new.write(line)





            share|improve this answer






























              2














              You're iterating through file2 len(file1) times which is certainly not what you want to do. You want to construct a replacement dictionary from file1 like so:



              import re

              # regex to find usernames.
              # You can use str.split to find the usernames like you did if you're
              # not comfortable with regular expressions.
              user_regex = re.compile(r'^${([a-zA-Z0-9_]+)}: ')

              # rename files to something better
              codes_file = "/home/user/scripts/temp/txt1"
              old_file = "/home/user/scripts/temp/txt2"
              new_file = "/home/user/scripts/temp/txt3"

              codes = {}
              with open(codes) as f: # use with to safely open files
              for line in f:
              match = user_regex.search(line)
              if match:
              codes[match.group(1)] = line

              # now we have the codes in ram for easy lookup

              with open(old_file) as old, open(new_file, 'w') as new:
              for line in old:
              match = user_regex.search(line)
              if match and match.group(1) in codes.keys():
              new.write(codes[match.group(1)])
              else:
              new.write(line)





              share|improve this answer




























                2












                2








                2







                You're iterating through file2 len(file1) times which is certainly not what you want to do. You want to construct a replacement dictionary from file1 like so:



                import re

                # regex to find usernames.
                # You can use str.split to find the usernames like you did if you're
                # not comfortable with regular expressions.
                user_regex = re.compile(r'^${([a-zA-Z0-9_]+)}: ')

                # rename files to something better
                codes_file = "/home/user/scripts/temp/txt1"
                old_file = "/home/user/scripts/temp/txt2"
                new_file = "/home/user/scripts/temp/txt3"

                codes = {}
                with open(codes) as f: # use with to safely open files
                for line in f:
                match = user_regex.search(line)
                if match:
                codes[match.group(1)] = line

                # now we have the codes in ram for easy lookup

                with open(old_file) as old, open(new_file, 'w') as new:
                for line in old:
                match = user_regex.search(line)
                if match and match.group(1) in codes.keys():
                new.write(codes[match.group(1)])
                else:
                new.write(line)





                share|improve this answer















                You're iterating through file2 len(file1) times which is certainly not what you want to do. You want to construct a replacement dictionary from file1 like so:



                import re

                # regex to find usernames.
                # You can use str.split to find the usernames like you did if you're
                # not comfortable with regular expressions.
                user_regex = re.compile(r'^${([a-zA-Z0-9_]+)}: ')

                # rename files to something better
                codes_file = "/home/user/scripts/temp/txt1"
                old_file = "/home/user/scripts/temp/txt2"
                new_file = "/home/user/scripts/temp/txt3"

                codes = {}
                with open(codes) as f: # use with to safely open files
                for line in f:
                match = user_regex.search(line)
                if match:
                codes[match.group(1)] = line

                # now we have the codes in ram for easy lookup

                with open(old_file) as old, open(new_file, 'w') as new:
                for line in old:
                match = user_regex.search(line)
                if match and match.group(1) in codes.keys():
                new.write(codes[match.group(1)])
                else:
                new.write(line)






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Jan 3 at 11:16

























                answered Jan 3 at 11:08









                FHTMitchellFHTMitchell

                8,34511229




                8,34511229
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54020946%2fpython-text-processing%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    MongoDB - Not Authorized To Execute Command

                    How to fix TextFormField cause rebuild widget in Flutter

                    in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith