How to read one character from a file as a key into a dictionary, then make the next n characters its value?
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
add a comment |
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
1
Awhile
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 at 22:31
Here's a hint: go to class and read your notes.
– Julien
Nov 12 at 22:34
Why would the "3" character following the "4" be skipped?
– martineau
Nov 12 at 23:21
add a comment |
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
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
python file dictionary file-read
edited Nov 12 at 23:18
martineau
65.5k988177
65.5k988177
asked Nov 12 at 22:28
bullebullebagarn
162
162
1
Awhile
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 at 22:31
Here's a hint: go to class and read your notes.
– Julien
Nov 12 at 22:34
Why would the "3" character following the "4" be skipped?
– martineau
Nov 12 at 23:21
add a comment |
1
Awhile
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 at 22:31
Here's a hint: go to class and read your notes.
– Julien
Nov 12 at 22:34
Why would the "3" character following the "4" be skipped?
– martineau
Nov 12 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 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 at 22:31
Here's a hint: go to class and read your notes.
– Julien
Nov 12 at 22:34
Here's a hint: go to class and read your notes.
– Julien
Nov 12 at 22:34
Why would the "3" character following the "4" be skipped?
– martineau
Nov 12 at 23:21
Why would the "3" character following the "4" be skipped?
– martineau
Nov 12 at 23:21
add a comment |
1 Answer
1
active
oldest
votes
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.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 12 at 23:30
Alvaro Bataller
515
515
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 at 22:31
Here's a hint: go to class and read your notes.
– Julien
Nov 12 at 22:34
Why would the "3" character following the "4" be skipped?
– martineau
Nov 12 at 23:21