Python - Reading through the same file from different starting lines





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







1















I am trying to read a .non file (you can find an example here).
Those files contains 4 keys: width, height, rows and columns (both are made of multiple values).



Width and Height are ALWAYS before Rows, Columns or in my case Goal, thus I have to go through the file while not knowing when I'll find what I need.



Here's what I'm trying to do:



# This function will create the grid
def fcreate(grid_id):
gridfile = open(grid_id['grid'], "r")
# Here, we're going through the entire file, getting the values we need.
# Still working on a pretty way to make the dictionary !
for line in gridfile:
if "width" in line:
grid_id['width'] = re.sub('[A-z]', '', line).strip()
if "height" in line:
grid_id['height'] = re.sub('[A-z]', '', line).strip()
if "rows" in line:
# Get all the rows values until something else ? Or EOF
if "columns" in line:
# Get all the columns values until something else ? Or EOF
# end of the for
gridfile.close()
return grid_id
pass


Grid_id contains all the values I have to get.



I've tried making a new for in the "if "rows"", a while and whatnot, but can't seem to escape either reading through the entire file again or looping on the line that contains "rows" or "columns.










share|improve this question































    1















    I am trying to read a .non file (you can find an example here).
    Those files contains 4 keys: width, height, rows and columns (both are made of multiple values).



    Width and Height are ALWAYS before Rows, Columns or in my case Goal, thus I have to go through the file while not knowing when I'll find what I need.



    Here's what I'm trying to do:



    # This function will create the grid
    def fcreate(grid_id):
    gridfile = open(grid_id['grid'], "r")
    # Here, we're going through the entire file, getting the values we need.
    # Still working on a pretty way to make the dictionary !
    for line in gridfile:
    if "width" in line:
    grid_id['width'] = re.sub('[A-z]', '', line).strip()
    if "height" in line:
    grid_id['height'] = re.sub('[A-z]', '', line).strip()
    if "rows" in line:
    # Get all the rows values until something else ? Or EOF
    if "columns" in line:
    # Get all the columns values until something else ? Or EOF
    # end of the for
    gridfile.close()
    return grid_id
    pass


    Grid_id contains all the values I have to get.



    I've tried making a new for in the "if "rows"", a while and whatnot, but can't seem to escape either reading through the entire file again or looping on the line that contains "rows" or "columns.










    share|improve this question



























      1












      1








      1








      I am trying to read a .non file (you can find an example here).
      Those files contains 4 keys: width, height, rows and columns (both are made of multiple values).



      Width and Height are ALWAYS before Rows, Columns or in my case Goal, thus I have to go through the file while not knowing when I'll find what I need.



      Here's what I'm trying to do:



      # This function will create the grid
      def fcreate(grid_id):
      gridfile = open(grid_id['grid'], "r")
      # Here, we're going through the entire file, getting the values we need.
      # Still working on a pretty way to make the dictionary !
      for line in gridfile:
      if "width" in line:
      grid_id['width'] = re.sub('[A-z]', '', line).strip()
      if "height" in line:
      grid_id['height'] = re.sub('[A-z]', '', line).strip()
      if "rows" in line:
      # Get all the rows values until something else ? Or EOF
      if "columns" in line:
      # Get all the columns values until something else ? Or EOF
      # end of the for
      gridfile.close()
      return grid_id
      pass


      Grid_id contains all the values I have to get.



      I've tried making a new for in the "if "rows"", a while and whatnot, but can't seem to escape either reading through the entire file again or looping on the line that contains "rows" or "columns.










      share|improve this question
















      I am trying to read a .non file (you can find an example here).
      Those files contains 4 keys: width, height, rows and columns (both are made of multiple values).



      Width and Height are ALWAYS before Rows, Columns or in my case Goal, thus I have to go through the file while not knowing when I'll find what I need.



      Here's what I'm trying to do:



      # This function will create the grid
      def fcreate(grid_id):
      gridfile = open(grid_id['grid'], "r")
      # Here, we're going through the entire file, getting the values we need.
      # Still working on a pretty way to make the dictionary !
      for line in gridfile:
      if "width" in line:
      grid_id['width'] = re.sub('[A-z]', '', line).strip()
      if "height" in line:
      grid_id['height'] = re.sub('[A-z]', '', line).strip()
      if "rows" in line:
      # Get all the rows values until something else ? Or EOF
      if "columns" in line:
      # Get all the columns values until something else ? Or EOF
      # end of the for
      gridfile.close()
      return grid_id
      pass


      Grid_id contains all the values I have to get.



      I've tried making a new for in the "if "rows"", a while and whatnot, but can't seem to escape either reading through the entire file again or looping on the line that contains "rows" or "columns.







      python






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 16 '18 at 10:31









      usr2564301

      17.9k73373




      17.9k73373










      asked Nov 16 '18 at 10:13









      KoreosKoreos

      103




      103
























          1 Answer
          1






          active

          oldest

          votes


















          0














          You can store the indexes of the "rows" and "columns" lines then use these values to get the rows and column values.
          You can do the following like that:



          lines = gridfile.readlines()
          for i,line in enumerate(lines):
          if "width" in line:
          grid_id['width'] = re.sub('[A-z]', '', line).strip()
          if "height" in line:
          grid_id['height'] = re.sub('[A-z]', '', line).strip()
          if "rows" in line:
          idx_rows = i
          if "columns" in line:
          idx_cols = i

          width = int(grid_id['width'])
          height = int(grid_id['height'])
          rows = lines[idx_rows+1:idx_rows+1+height]
          cols = lines[idx_cols+1:idx_cols+1+width]





          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%2f53335653%2fpython-reading-through-the-same-file-from-different-starting-lines%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









            0














            You can store the indexes of the "rows" and "columns" lines then use these values to get the rows and column values.
            You can do the following like that:



            lines = gridfile.readlines()
            for i,line in enumerate(lines):
            if "width" in line:
            grid_id['width'] = re.sub('[A-z]', '', line).strip()
            if "height" in line:
            grid_id['height'] = re.sub('[A-z]', '', line).strip()
            if "rows" in line:
            idx_rows = i
            if "columns" in line:
            idx_cols = i

            width = int(grid_id['width'])
            height = int(grid_id['height'])
            rows = lines[idx_rows+1:idx_rows+1+height]
            cols = lines[idx_cols+1:idx_cols+1+width]





            share|improve this answer






























              0














              You can store the indexes of the "rows" and "columns" lines then use these values to get the rows and column values.
              You can do the following like that:



              lines = gridfile.readlines()
              for i,line in enumerate(lines):
              if "width" in line:
              grid_id['width'] = re.sub('[A-z]', '', line).strip()
              if "height" in line:
              grid_id['height'] = re.sub('[A-z]', '', line).strip()
              if "rows" in line:
              idx_rows = i
              if "columns" in line:
              idx_cols = i

              width = int(grid_id['width'])
              height = int(grid_id['height'])
              rows = lines[idx_rows+1:idx_rows+1+height]
              cols = lines[idx_cols+1:idx_cols+1+width]





              share|improve this answer




























                0












                0








                0







                You can store the indexes of the "rows" and "columns" lines then use these values to get the rows and column values.
                You can do the following like that:



                lines = gridfile.readlines()
                for i,line in enumerate(lines):
                if "width" in line:
                grid_id['width'] = re.sub('[A-z]', '', line).strip()
                if "height" in line:
                grid_id['height'] = re.sub('[A-z]', '', line).strip()
                if "rows" in line:
                idx_rows = i
                if "columns" in line:
                idx_cols = i

                width = int(grid_id['width'])
                height = int(grid_id['height'])
                rows = lines[idx_rows+1:idx_rows+1+height]
                cols = lines[idx_cols+1:idx_cols+1+width]





                share|improve this answer















                You can store the indexes of the "rows" and "columns" lines then use these values to get the rows and column values.
                You can do the following like that:



                lines = gridfile.readlines()
                for i,line in enumerate(lines):
                if "width" in line:
                grid_id['width'] = re.sub('[A-z]', '', line).strip()
                if "height" in line:
                grid_id['height'] = re.sub('[A-z]', '', line).strip()
                if "rows" in line:
                idx_rows = i
                if "columns" in line:
                idx_cols = i

                width = int(grid_id['width'])
                height = int(grid_id['height'])
                rows = lines[idx_rows+1:idx_rows+1+height]
                cols = lines[idx_cols+1:idx_cols+1+width]






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 16 '18 at 10:34

























                answered Nov 16 '18 at 10:16









                Mael GalliffetMael Galliffet

                1364




                1364
































                    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%2f53335653%2fpython-reading-through-the-same-file-from-different-starting-lines%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

                    Florida Star v. B. J. F.

                    Error while running script in elastic search , gateway timeout

                    Adding quotations to stringified JSON object values