How to read one character from a file as a key into a dictionary, then make the next n characters its value?










-1














Lets say we have a .txt file like so (all on one line):



A2.)43@|@C3::#


So we want "A" as key for the value ".)" and "4" as key for the value "@|@" etc.. The number after the key tells us how many characters to read as the value, and the character after is the key for the next item in the dictionary.



The thing I'm struggling with is writing the loop. I'm thinking that we might want to iterate over the length of the file using a for loop. But I don't really know how to proceed.










share|improve this question



















  • 1




    A while loop would be better so you can increment the index variable arbitrarily (or check for end of file instead) to process all characters of a value at once.
    – Michael Butscher
    Nov 12 '18 at 22:31











  • Here's a hint: go to class and read your notes.
    – Julien
    Nov 12 '18 at 22:34










  • Why would the "3" character following the "4" be skipped?
    – martineau
    Nov 12 '18 at 23:21















-1














Lets say we have a .txt file like so (all on one line):



A2.)43@|@C3::#


So we want "A" as key for the value ".)" and "4" as key for the value "@|@" etc.. The number after the key tells us how many characters to read as the value, and the character after is the key for the next item in the dictionary.



The thing I'm struggling with is writing the loop. I'm thinking that we might want to iterate over the length of the file using a for loop. But I don't really know how to proceed.










share|improve this question



















  • 1




    A while loop would be better so you can increment the index variable arbitrarily (or check for end of file instead) to process all characters of a value at once.
    – Michael Butscher
    Nov 12 '18 at 22:31











  • Here's a hint: go to class and read your notes.
    – Julien
    Nov 12 '18 at 22:34










  • Why would the "3" character following the "4" be skipped?
    – martineau
    Nov 12 '18 at 23:21













-1












-1








-1







Lets say we have a .txt file like so (all on one line):



A2.)43@|@C3::#


So we want "A" as key for the value ".)" and "4" as key for the value "@|@" etc.. The number after the key tells us how many characters to read as the value, and the character after is the key for the next item in the dictionary.



The thing I'm struggling with is writing the loop. I'm thinking that we might want to iterate over the length of the file using a for loop. But I don't really know how to proceed.










share|improve this question















Lets say we have a .txt file like so (all on one line):



A2.)43@|@C3::#


So we want "A" as key for the value ".)" and "4" as key for the value "@|@" etc.. The number after the key tells us how many characters to read as the value, and the character after is the key for the next item in the dictionary.



The thing I'm struggling with is writing the loop. I'm thinking that we might want to iterate over the length of the file using a for loop. But I don't really know how to proceed.







python file dictionary file-read






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 23:18









martineau

65.9k989177




65.9k989177










asked Nov 12 '18 at 22:28









bullebullebagarn

162




162







  • 1




    A while loop would be better so you can increment the index variable arbitrarily (or check for end of file instead) to process all characters of a value at once.
    – Michael Butscher
    Nov 12 '18 at 22:31











  • Here's a hint: go to class and read your notes.
    – Julien
    Nov 12 '18 at 22:34










  • Why would the "3" character following the "4" be skipped?
    – martineau
    Nov 12 '18 at 23:21












  • 1




    A while loop would be better so you can increment the index variable arbitrarily (or check for end of file instead) to process all characters of a value at once.
    – Michael Butscher
    Nov 12 '18 at 22:31











  • Here's a hint: go to class and read your notes.
    – Julien
    Nov 12 '18 at 22:34










  • Why would the "3" character following the "4" be skipped?
    – martineau
    Nov 12 '18 at 23:21







1




1




A while loop would be better so you can increment the index variable arbitrarily (or check for end of file instead) to process all characters of a value at once.
– Michael Butscher
Nov 12 '18 at 22:31





A while loop would be better so you can increment the index variable arbitrarily (or check for end of file instead) to process all characters of a value at once.
– Michael Butscher
Nov 12 '18 at 22:31













Here's a hint: go to class and read your notes.
– Julien
Nov 12 '18 at 22:34




Here's a hint: go to class and read your notes.
– Julien
Nov 12 '18 at 22:34












Why would the "3" character following the "4" be skipped?
– martineau
Nov 12 '18 at 23:21




Why would the "3" character following the "4" be skipped?
– martineau
Nov 12 '18 at 23:21












1 Answer
1






active

oldest

votes


















0














This worked for me



stringText = "A2.)43@|@C3::#"


i=0
myDictionary =


while True:
try:
#First we grab the key character using i
keyCharacter = stringText[i]

#Then we grab the next character which will tell us how many characters they value will have
# We also convert it to integer for future use
valueLength = int(stringText[i+1])

#Then we grab the value by slicing the string
valueCharacters = stringText[i+2:i+2+valueLength]

#We add this to the dictionary
myDictionary[keyCharacter] = valueCharacters

#We update i to continue where we left off
i = i+2+valueLength


except IndexError:
break

print myDictionary


Instead of the "try and except" you could also see how long that string is and do and if statement to break the loop when i is bigger than the length of the string.






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%2f53271009%2fhow-to-read-one-character-from-a-file-as-a-key-into-a-dictionary-then-make-the%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














    This worked for me



    stringText = "A2.)43@|@C3::#"


    i=0
    myDictionary =


    while True:
    try:
    #First we grab the key character using i
    keyCharacter = stringText[i]

    #Then we grab the next character which will tell us how many characters they value will have
    # We also convert it to integer for future use
    valueLength = int(stringText[i+1])

    #Then we grab the value by slicing the string
    valueCharacters = stringText[i+2:i+2+valueLength]

    #We add this to the dictionary
    myDictionary[keyCharacter] = valueCharacters

    #We update i to continue where we left off
    i = i+2+valueLength


    except IndexError:
    break

    print myDictionary


    Instead of the "try and except" you could also see how long that string is and do and if statement to break the loop when i is bigger than the length of the string.






    share|improve this answer

























      0














      This worked for me



      stringText = "A2.)43@|@C3::#"


      i=0
      myDictionary =


      while True:
      try:
      #First we grab the key character using i
      keyCharacter = stringText[i]

      #Then we grab the next character which will tell us how many characters they value will have
      # We also convert it to integer for future use
      valueLength = int(stringText[i+1])

      #Then we grab the value by slicing the string
      valueCharacters = stringText[i+2:i+2+valueLength]

      #We add this to the dictionary
      myDictionary[keyCharacter] = valueCharacters

      #We update i to continue where we left off
      i = i+2+valueLength


      except IndexError:
      break

      print myDictionary


      Instead of the "try and except" you could also see how long that string is and do and if statement to break the loop when i is bigger than the length of the string.






      share|improve this answer























        0












        0








        0






        This worked for me



        stringText = "A2.)43@|@C3::#"


        i=0
        myDictionary =


        while True:
        try:
        #First we grab the key character using i
        keyCharacter = stringText[i]

        #Then we grab the next character which will tell us how many characters they value will have
        # We also convert it to integer for future use
        valueLength = int(stringText[i+1])

        #Then we grab the value by slicing the string
        valueCharacters = stringText[i+2:i+2+valueLength]

        #We add this to the dictionary
        myDictionary[keyCharacter] = valueCharacters

        #We update i to continue where we left off
        i = i+2+valueLength


        except IndexError:
        break

        print myDictionary


        Instead of the "try and except" you could also see how long that string is and do and if statement to break the loop when i is bigger than the length of the string.






        share|improve this answer












        This worked for me



        stringText = "A2.)43@|@C3::#"


        i=0
        myDictionary =


        while True:
        try:
        #First we grab the key character using i
        keyCharacter = stringText[i]

        #Then we grab the next character which will tell us how many characters they value will have
        # We also convert it to integer for future use
        valueLength = int(stringText[i+1])

        #Then we grab the value by slicing the string
        valueCharacters = stringText[i+2:i+2+valueLength]

        #We add this to the dictionary
        myDictionary[keyCharacter] = valueCharacters

        #We update i to continue where we left off
        i = i+2+valueLength


        except IndexError:
        break

        print myDictionary


        Instead of the "try and except" you could also see how long that string is and do and if statement to break the loop when i is bigger than the length of the string.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 12 '18 at 23:30









        Alvaro Bataller

        515




        515



























            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2f53271009%2fhow-to-read-one-character-from-a-file-as-a-key-into-a-dictionary-then-make-the%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

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            ReactJS Fetched API data displays live - need Data displayed static

            政党