Entry of a list item in a file





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







0















Good afternoon, I have a multiple list of IP and MAC, list of arbitrary length



A = [['10.0.0.1','00:4C:3S:**:**:**', 0], ['10.0.0.2', '00:5C:4S:**:**:**', 0], [....], [....]]


I want to check if this MAC is in the oui file:



E043DB   (base 16)   Shenzhen
2405f5 (base 16) Integrated
3CD92B (base 16) Hewlett Packard
...


If the MAC from the list is in the file, write the name of the manufacturer as 3 list items. I'm trying to do so and it turns out to check only the first element, the remaining ones are not checked, how can I do this please tell me?



f = open('oui.txt', 'r')
for values in A:
for line in f.readlines():
if values[1][0:8].replace(':','') in line:
values[2]=(line.split('(base 16)')[1].strip())
f.close()
print (A)


And get an answer:



A = [['10.0.0.1','00:4C:3S:**:**:**', 'Firm Name'], ['10.0.0.2', '00:5C:4S:**:**:**', 0], [....], [....]]









share|improve this question





























    0















    Good afternoon, I have a multiple list of IP and MAC, list of arbitrary length



    A = [['10.0.0.1','00:4C:3S:**:**:**', 0], ['10.0.0.2', '00:5C:4S:**:**:**', 0], [....], [....]]


    I want to check if this MAC is in the oui file:



    E043DB   (base 16)   Shenzhen
    2405f5 (base 16) Integrated
    3CD92B (base 16) Hewlett Packard
    ...


    If the MAC from the list is in the file, write the name of the manufacturer as 3 list items. I'm trying to do so and it turns out to check only the first element, the remaining ones are not checked, how can I do this please tell me?



    f = open('oui.txt', 'r')
    for values in A:
    for line in f.readlines():
    if values[1][0:8].replace(':','') in line:
    values[2]=(line.split('(base 16)')[1].strip())
    f.close()
    print (A)


    And get an answer:



    A = [['10.0.0.1','00:4C:3S:**:**:**', 'Firm Name'], ['10.0.0.2', '00:5C:4S:**:**:**', 0], [....], [....]]









    share|improve this question

























      0












      0








      0








      Good afternoon, I have a multiple list of IP and MAC, list of arbitrary length



      A = [['10.0.0.1','00:4C:3S:**:**:**', 0], ['10.0.0.2', '00:5C:4S:**:**:**', 0], [....], [....]]


      I want to check if this MAC is in the oui file:



      E043DB   (base 16)   Shenzhen
      2405f5 (base 16) Integrated
      3CD92B (base 16) Hewlett Packard
      ...


      If the MAC from the list is in the file, write the name of the manufacturer as 3 list items. I'm trying to do so and it turns out to check only the first element, the remaining ones are not checked, how can I do this please tell me?



      f = open('oui.txt', 'r')
      for values in A:
      for line in f.readlines():
      if values[1][0:8].replace(':','') in line:
      values[2]=(line.split('(base 16)')[1].strip())
      f.close()
      print (A)


      And get an answer:



      A = [['10.0.0.1','00:4C:3S:**:**:**', 'Firm Name'], ['10.0.0.2', '00:5C:4S:**:**:**', 0], [....], [....]]









      share|improve this question














      Good afternoon, I have a multiple list of IP and MAC, list of arbitrary length



      A = [['10.0.0.1','00:4C:3S:**:**:**', 0], ['10.0.0.2', '00:5C:4S:**:**:**', 0], [....], [....]]


      I want to check if this MAC is in the oui file:



      E043DB   (base 16)   Shenzhen
      2405f5 (base 16) Integrated
      3CD92B (base 16) Hewlett Packard
      ...


      If the MAC from the list is in the file, write the name of the manufacturer as 3 list items. I'm trying to do so and it turns out to check only the first element, the remaining ones are not checked, how can I do this please tell me?



      f = open('oui.txt', 'r')
      for values in A:
      for line in f.readlines():
      if values[1][0:8].replace(':','') in line:
      values[2]=(line.split('(base 16)')[1].strip())
      f.close()
      print (A)


      And get an answer:



      A = [['10.0.0.1','00:4C:3S:**:**:**', 'Firm Name'], ['10.0.0.2', '00:5C:4S:**:**:**', 0], [....], [....]]






      python python-3.x list readfile






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 22 '18 at 15:12









      VasaiVasai

      215




      215
























          2 Answers
          2






          active

          oldest

          votes


















          0














          The Problem



          Consider the "shape" of your code:



          f = open('a file')
          for values in [ 'some list' ]:
          for line in f.readlines():


          Your two loops are doing this:



          Start with first value in list
          Read all lines remaining in file object f
          Move to next value in list
          Read all lines remaining in file object f


          Except that the first time you told it to "read all lines remaining" it would do so.



          So, unless you have some way to put more lines into f (which can happen with async files like stdin!) you are going to get one "good" pass through the file, and then every subsequent pass the file object will point to the end of the file, so you'll get nothing.



          A Solution



          When you are dealing with a file, you want to only process it one time. File I/O is expensive compared to other operations. So you can choose to either (a) read the entire file into memory, and do whatever you want since it's not a file any more; or (b) scan through it only one time.



          If you choose to scan through it only once, the easy solution is just to invert the two for loops. Instead of doing this:



          for item in list:
          for line in file:


          Do this instead:



          for line in file:
          for item in list:


          And presto! You are now only reading the file one time.



          Other Considerations



          If I look at your code, and your examples, it seems like you are trying for an exact match on a particular key. You trim down the MAC addresses in your list to check them against the manufacturer ids.



          This suggests to me that you might well have many, many more list values (source MAC addresses) than you have manufacturers. So perhaps you should consider reading the contents of the tile into memory, rather than processing it one line at a time.



          Once you have the file in memory, consider building a proper dictionary. You have a key (MAC prefix) and a value (manufacturer). So build something like:



          for line in f:
          mac = line.split('(base 16)')[0].strip()
          mfg = line.split('(base 16)')[1].strip()
          mac_to_mfg[mac] = mfg


          Then you can make one pass through the source addresses and use the dict's O(1) lookup to your advantage:



          for src in A:
          prefix = src[1][:8].replace(':', '')
          if prefix in mac_to_mfg:
          # etc...





          share|improve this answer
























          • I did it, it works, thanks!

            – Vasai
            Nov 23 '18 at 13:13





















          0














          The problem is you got the order of the loops reversed. Usually this isn't that big of a problem, but when working objects that are consumed (like the IO file object) the contents will no longer produce once it's been iterated over.



          You'll need to iterate the lines first, and then within each lines iterate through A to check the values:



          with open('oui.txt', 'r') as f:
          for line in f.readlines():
          for values in A:
          if values[1][0:8].replace(':','') in line:
          values[2]=(line.split('(base 16)')[1].strip())

          print (A)


          Notice I changed your file opening to use the with context manager instead, where once your code exists the with block it will automatically close() the file for you. It is recommended over manually opening the file as you might forget to close() it after.






          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%2f53433841%2fentry-of-a-list-item-in-a-file%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            The Problem



            Consider the "shape" of your code:



            f = open('a file')
            for values in [ 'some list' ]:
            for line in f.readlines():


            Your two loops are doing this:



            Start with first value in list
            Read all lines remaining in file object f
            Move to next value in list
            Read all lines remaining in file object f


            Except that the first time you told it to "read all lines remaining" it would do so.



            So, unless you have some way to put more lines into f (which can happen with async files like stdin!) you are going to get one "good" pass through the file, and then every subsequent pass the file object will point to the end of the file, so you'll get nothing.



            A Solution



            When you are dealing with a file, you want to only process it one time. File I/O is expensive compared to other operations. So you can choose to either (a) read the entire file into memory, and do whatever you want since it's not a file any more; or (b) scan through it only one time.



            If you choose to scan through it only once, the easy solution is just to invert the two for loops. Instead of doing this:



            for item in list:
            for line in file:


            Do this instead:



            for line in file:
            for item in list:


            And presto! You are now only reading the file one time.



            Other Considerations



            If I look at your code, and your examples, it seems like you are trying for an exact match on a particular key. You trim down the MAC addresses in your list to check them against the manufacturer ids.



            This suggests to me that you might well have many, many more list values (source MAC addresses) than you have manufacturers. So perhaps you should consider reading the contents of the tile into memory, rather than processing it one line at a time.



            Once you have the file in memory, consider building a proper dictionary. You have a key (MAC prefix) and a value (manufacturer). So build something like:



            for line in f:
            mac = line.split('(base 16)')[0].strip()
            mfg = line.split('(base 16)')[1].strip()
            mac_to_mfg[mac] = mfg


            Then you can make one pass through the source addresses and use the dict's O(1) lookup to your advantage:



            for src in A:
            prefix = src[1][:8].replace(':', '')
            if prefix in mac_to_mfg:
            # etc...





            share|improve this answer
























            • I did it, it works, thanks!

              – Vasai
              Nov 23 '18 at 13:13


















            0














            The Problem



            Consider the "shape" of your code:



            f = open('a file')
            for values in [ 'some list' ]:
            for line in f.readlines():


            Your two loops are doing this:



            Start with first value in list
            Read all lines remaining in file object f
            Move to next value in list
            Read all lines remaining in file object f


            Except that the first time you told it to "read all lines remaining" it would do so.



            So, unless you have some way to put more lines into f (which can happen with async files like stdin!) you are going to get one "good" pass through the file, and then every subsequent pass the file object will point to the end of the file, so you'll get nothing.



            A Solution



            When you are dealing with a file, you want to only process it one time. File I/O is expensive compared to other operations. So you can choose to either (a) read the entire file into memory, and do whatever you want since it's not a file any more; or (b) scan through it only one time.



            If you choose to scan through it only once, the easy solution is just to invert the two for loops. Instead of doing this:



            for item in list:
            for line in file:


            Do this instead:



            for line in file:
            for item in list:


            And presto! You are now only reading the file one time.



            Other Considerations



            If I look at your code, and your examples, it seems like you are trying for an exact match on a particular key. You trim down the MAC addresses in your list to check them against the manufacturer ids.



            This suggests to me that you might well have many, many more list values (source MAC addresses) than you have manufacturers. So perhaps you should consider reading the contents of the tile into memory, rather than processing it one line at a time.



            Once you have the file in memory, consider building a proper dictionary. You have a key (MAC prefix) and a value (manufacturer). So build something like:



            for line in f:
            mac = line.split('(base 16)')[0].strip()
            mfg = line.split('(base 16)')[1].strip()
            mac_to_mfg[mac] = mfg


            Then you can make one pass through the source addresses and use the dict's O(1) lookup to your advantage:



            for src in A:
            prefix = src[1][:8].replace(':', '')
            if prefix in mac_to_mfg:
            # etc...





            share|improve this answer
























            • I did it, it works, thanks!

              – Vasai
              Nov 23 '18 at 13:13
















            0












            0








            0







            The Problem



            Consider the "shape" of your code:



            f = open('a file')
            for values in [ 'some list' ]:
            for line in f.readlines():


            Your two loops are doing this:



            Start with first value in list
            Read all lines remaining in file object f
            Move to next value in list
            Read all lines remaining in file object f


            Except that the first time you told it to "read all lines remaining" it would do so.



            So, unless you have some way to put more lines into f (which can happen with async files like stdin!) you are going to get one "good" pass through the file, and then every subsequent pass the file object will point to the end of the file, so you'll get nothing.



            A Solution



            When you are dealing with a file, you want to only process it one time. File I/O is expensive compared to other operations. So you can choose to either (a) read the entire file into memory, and do whatever you want since it's not a file any more; or (b) scan through it only one time.



            If you choose to scan through it only once, the easy solution is just to invert the two for loops. Instead of doing this:



            for item in list:
            for line in file:


            Do this instead:



            for line in file:
            for item in list:


            And presto! You are now only reading the file one time.



            Other Considerations



            If I look at your code, and your examples, it seems like you are trying for an exact match on a particular key. You trim down the MAC addresses in your list to check them against the manufacturer ids.



            This suggests to me that you might well have many, many more list values (source MAC addresses) than you have manufacturers. So perhaps you should consider reading the contents of the tile into memory, rather than processing it one line at a time.



            Once you have the file in memory, consider building a proper dictionary. You have a key (MAC prefix) and a value (manufacturer). So build something like:



            for line in f:
            mac = line.split('(base 16)')[0].strip()
            mfg = line.split('(base 16)')[1].strip()
            mac_to_mfg[mac] = mfg


            Then you can make one pass through the source addresses and use the dict's O(1) lookup to your advantage:



            for src in A:
            prefix = src[1][:8].replace(':', '')
            if prefix in mac_to_mfg:
            # etc...





            share|improve this answer













            The Problem



            Consider the "shape" of your code:



            f = open('a file')
            for values in [ 'some list' ]:
            for line in f.readlines():


            Your two loops are doing this:



            Start with first value in list
            Read all lines remaining in file object f
            Move to next value in list
            Read all lines remaining in file object f


            Except that the first time you told it to "read all lines remaining" it would do so.



            So, unless you have some way to put more lines into f (which can happen with async files like stdin!) you are going to get one "good" pass through the file, and then every subsequent pass the file object will point to the end of the file, so you'll get nothing.



            A Solution



            When you are dealing with a file, you want to only process it one time. File I/O is expensive compared to other operations. So you can choose to either (a) read the entire file into memory, and do whatever you want since it's not a file any more; or (b) scan through it only one time.



            If you choose to scan through it only once, the easy solution is just to invert the two for loops. Instead of doing this:



            for item in list:
            for line in file:


            Do this instead:



            for line in file:
            for item in list:


            And presto! You are now only reading the file one time.



            Other Considerations



            If I look at your code, and your examples, it seems like you are trying for an exact match on a particular key. You trim down the MAC addresses in your list to check them against the manufacturer ids.



            This suggests to me that you might well have many, many more list values (source MAC addresses) than you have manufacturers. So perhaps you should consider reading the contents of the tile into memory, rather than processing it one line at a time.



            Once you have the file in memory, consider building a proper dictionary. You have a key (MAC prefix) and a value (manufacturer). So build something like:



            for line in f:
            mac = line.split('(base 16)')[0].strip()
            mfg = line.split('(base 16)')[1].strip()
            mac_to_mfg[mac] = mfg


            Then you can make one pass through the source addresses and use the dict's O(1) lookup to your advantage:



            for src in A:
            prefix = src[1][:8].replace(':', '')
            if prefix in mac_to_mfg:
            # etc...






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 22 '18 at 15:37









            aghastaghast

            11.3k11135




            11.3k11135













            • I did it, it works, thanks!

              – Vasai
              Nov 23 '18 at 13:13





















            • I did it, it works, thanks!

              – Vasai
              Nov 23 '18 at 13:13



















            I did it, it works, thanks!

            – Vasai
            Nov 23 '18 at 13:13







            I did it, it works, thanks!

            – Vasai
            Nov 23 '18 at 13:13















            0














            The problem is you got the order of the loops reversed. Usually this isn't that big of a problem, but when working objects that are consumed (like the IO file object) the contents will no longer produce once it's been iterated over.



            You'll need to iterate the lines first, and then within each lines iterate through A to check the values:



            with open('oui.txt', 'r') as f:
            for line in f.readlines():
            for values in A:
            if values[1][0:8].replace(':','') in line:
            values[2]=(line.split('(base 16)')[1].strip())

            print (A)


            Notice I changed your file opening to use the with context manager instead, where once your code exists the with block it will automatically close() the file for you. It is recommended over manually opening the file as you might forget to close() it after.






            share|improve this answer




























              0














              The problem is you got the order of the loops reversed. Usually this isn't that big of a problem, but when working objects that are consumed (like the IO file object) the contents will no longer produce once it's been iterated over.



              You'll need to iterate the lines first, and then within each lines iterate through A to check the values:



              with open('oui.txt', 'r') as f:
              for line in f.readlines():
              for values in A:
              if values[1][0:8].replace(':','') in line:
              values[2]=(line.split('(base 16)')[1].strip())

              print (A)


              Notice I changed your file opening to use the with context manager instead, where once your code exists the with block it will automatically close() the file for you. It is recommended over manually opening the file as you might forget to close() it after.






              share|improve this answer


























                0












                0








                0







                The problem is you got the order of the loops reversed. Usually this isn't that big of a problem, but when working objects that are consumed (like the IO file object) the contents will no longer produce once it's been iterated over.



                You'll need to iterate the lines first, and then within each lines iterate through A to check the values:



                with open('oui.txt', 'r') as f:
                for line in f.readlines():
                for values in A:
                if values[1][0:8].replace(':','') in line:
                values[2]=(line.split('(base 16)')[1].strip())

                print (A)


                Notice I changed your file opening to use the with context manager instead, where once your code exists the with block it will automatically close() the file for you. It is recommended over manually opening the file as you might forget to close() it after.






                share|improve this answer













                The problem is you got the order of the loops reversed. Usually this isn't that big of a problem, but when working objects that are consumed (like the IO file object) the contents will no longer produce once it's been iterated over.



                You'll need to iterate the lines first, and then within each lines iterate through A to check the values:



                with open('oui.txt', 'r') as f:
                for line in f.readlines():
                for values in A:
                if values[1][0:8].replace(':','') in line:
                values[2]=(line.split('(base 16)')[1].strip())

                print (A)


                Notice I changed your file opening to use the with context manager instead, where once your code exists the with block it will automatically close() the file for you. It is recommended over manually opening the file as you might forget to close() it after.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 22 '18 at 15:32









                IdlehandsIdlehands

                6,1631923




                6,1631923






























                    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%2f53433841%2fentry-of-a-list-item-in-a-file%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

                    mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

                    How to change which sound is reproduced for terminal bell?

                    Can I use Tabulator js library in my java Spring + Thymeleaf project?