Use dictionary value to parse a string in pythonHow do I merge two dictionaries in a single expression?Calling an external command from PythonWhat are metaclasses in Python?How do I parse a string to a float or int?Does Python have a ternary conditional operator?How do I sort a dictionary by value?Add new keys to a dictionary?Check if a given key already exists in a dictionaryIterating over dictionaries using 'for' loopsDoes Python have a string 'contains' substring method?
Passport expiration requirement for Jordan Visa
Is this a pure function?
Is it a good idea to contact a candidate?
Making Sandwiches
Iodine tablet correct use and efficiency
What is the meaning of Text inside of AMS logo
What are standard cryptographic assumptions?
Is it sportsmanlike to waste opponents' time by giving check at the end of the game?
How can I kill/separate a Deity from the Prime Material Plane?
Does SQL Server Only Perform Calculations In A SELECT List Once?
"Chess is 90% tactics" - should a player focus more on tactics in order to improve?
Polling on Impeachment
Basic Accidental Question
how to make a twisted wrapper
Is there any point in having more than 6 months' runway in savings?
Musig Signature Interactivity
I have stack-exchanged through my undergrad math program. Am I likely to succeed in Mathematics PhD programs?
Implement the Max-Pooling operation from Convolutional Neural Networks
What happened to Sophie in her last encounter with Arthur?
What do you call someone whose unmarried partner has died?
how do i play 2 notes high and low together with a plectrum on guitar
Very high precision zero crossing detection
Can I request a credit item be removed from my report as soon as it is paid in full?
Hole in PCB due to corrosive reaction?
Use dictionary value to parse a string in python
How do I merge two dictionaries in a single expression?Calling an external command from PythonWhat are metaclasses in Python?How do I parse a string to a float or int?Does Python have a ternary conditional operator?How do I sort a dictionary by value?Add new keys to a dictionary?Check if a given key already exists in a dictionaryIterating over dictionaries using 'for' loopsDoes Python have a string 'contains' substring method?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;
I'm trying to parse a string at a specified position which is pulled from a dictionary. I've only been working with python for a couple months and I can't figure out how to pull in the value correctly.
I've tried finding answers within posts about dictionaries, but I'm not seeing this specific question.
#pull in mods, excel file, and sheets needed
sourceIdDict=
for x in range(10):
source=str(insheet2.cell(x,0))
numBgn=insheet2.cell(x,4)
sourceIdDict[source]=numBgn
for count in range(500):
crs=str(insheet1.cell(count,0))
src=str(insheet1.cell(count,1))
yrtr=str(insheet1.cell(count,2))
tseg=str(insheet1.cell(count,3))
uSeg=str(insheet1.cell(count,4))
level=crs[sourceIdDict.get(src)]
#if condition met: write to output file
#close file
The last line seems to be the problem. I keep getting TypeError: string indices must be integers.
I don't know why it isn't reading it as an integer, even when I use crsBgn=int(insheet2.cell(x,4))
. source is the same info as src.
The other variables are string because they could have leading zeros, which I don't want dropped when it's written into excel.
python parsing dictionary
|
show 2 more comments
I'm trying to parse a string at a specified position which is pulled from a dictionary. I've only been working with python for a couple months and I can't figure out how to pull in the value correctly.
I've tried finding answers within posts about dictionaries, but I'm not seeing this specific question.
#pull in mods, excel file, and sheets needed
sourceIdDict=
for x in range(10):
source=str(insheet2.cell(x,0))
numBgn=insheet2.cell(x,4)
sourceIdDict[source]=numBgn
for count in range(500):
crs=str(insheet1.cell(count,0))
src=str(insheet1.cell(count,1))
yrtr=str(insheet1.cell(count,2))
tseg=str(insheet1.cell(count,3))
uSeg=str(insheet1.cell(count,4))
level=crs[sourceIdDict.get(src)]
#if condition met: write to output file
#close file
The last line seems to be the problem. I keep getting TypeError: string indices must be integers.
I don't know why it isn't reading it as an integer, even when I use crsBgn=int(insheet2.cell(x,4))
. source is the same info as src.
The other variables are string because they could have leading zeros, which I don't want dropped when it's written into excel.
python parsing dictionary
Does type casting work? I.e. convertingsourceIdDict.get(src)
toint(sourceIdDict.get(src))
?
– feliks
Mar 28 at 22:03
You may be getting a non-integer from the dict or a None if the key isn't present, it depends on the input. I think you should replace the last line of first for bysourceIdDict[source]=int(numBgn)
– geckos
Mar 28 at 22:06
You may get TypeErrors if numBgn can't be parsed, surround it by try/catch and handle it accordingly
– geckos
Mar 28 at 22:08
@feliks No, then the error changes to TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'
– anythingButThat
Mar 29 at 18:25
@geckos That gives TypeError: int() argument must be a string, a bytes-like object or a number, not 'Cell'
– anythingButThat
Mar 29 at 18:28
|
show 2 more comments
I'm trying to parse a string at a specified position which is pulled from a dictionary. I've only been working with python for a couple months and I can't figure out how to pull in the value correctly.
I've tried finding answers within posts about dictionaries, but I'm not seeing this specific question.
#pull in mods, excel file, and sheets needed
sourceIdDict=
for x in range(10):
source=str(insheet2.cell(x,0))
numBgn=insheet2.cell(x,4)
sourceIdDict[source]=numBgn
for count in range(500):
crs=str(insheet1.cell(count,0))
src=str(insheet1.cell(count,1))
yrtr=str(insheet1.cell(count,2))
tseg=str(insheet1.cell(count,3))
uSeg=str(insheet1.cell(count,4))
level=crs[sourceIdDict.get(src)]
#if condition met: write to output file
#close file
The last line seems to be the problem. I keep getting TypeError: string indices must be integers.
I don't know why it isn't reading it as an integer, even when I use crsBgn=int(insheet2.cell(x,4))
. source is the same info as src.
The other variables are string because they could have leading zeros, which I don't want dropped when it's written into excel.
python parsing dictionary
I'm trying to parse a string at a specified position which is pulled from a dictionary. I've only been working with python for a couple months and I can't figure out how to pull in the value correctly.
I've tried finding answers within posts about dictionaries, but I'm not seeing this specific question.
#pull in mods, excel file, and sheets needed
sourceIdDict=
for x in range(10):
source=str(insheet2.cell(x,0))
numBgn=insheet2.cell(x,4)
sourceIdDict[source]=numBgn
for count in range(500):
crs=str(insheet1.cell(count,0))
src=str(insheet1.cell(count,1))
yrtr=str(insheet1.cell(count,2))
tseg=str(insheet1.cell(count,3))
uSeg=str(insheet1.cell(count,4))
level=crs[sourceIdDict.get(src)]
#if condition met: write to output file
#close file
The last line seems to be the problem. I keep getting TypeError: string indices must be integers.
I don't know why it isn't reading it as an integer, even when I use crsBgn=int(insheet2.cell(x,4))
. source is the same info as src.
The other variables are string because they could have leading zeros, which I don't want dropped when it's written into excel.
python parsing dictionary
python parsing dictionary
edited Mar 28 at 22:37
Aman Ojha
32 bronze badges
32 bronze badges
asked Mar 28 at 21:59
anythingButThatanythingButThat
11 bronze badge
11 bronze badge
Does type casting work? I.e. convertingsourceIdDict.get(src)
toint(sourceIdDict.get(src))
?
– feliks
Mar 28 at 22:03
You may be getting a non-integer from the dict or a None if the key isn't present, it depends on the input. I think you should replace the last line of first for bysourceIdDict[source]=int(numBgn)
– geckos
Mar 28 at 22:06
You may get TypeErrors if numBgn can't be parsed, surround it by try/catch and handle it accordingly
– geckos
Mar 28 at 22:08
@feliks No, then the error changes to TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'
– anythingButThat
Mar 29 at 18:25
@geckos That gives TypeError: int() argument must be a string, a bytes-like object or a number, not 'Cell'
– anythingButThat
Mar 29 at 18:28
|
show 2 more comments
Does type casting work? I.e. convertingsourceIdDict.get(src)
toint(sourceIdDict.get(src))
?
– feliks
Mar 28 at 22:03
You may be getting a non-integer from the dict or a None if the key isn't present, it depends on the input. I think you should replace the last line of first for bysourceIdDict[source]=int(numBgn)
– geckos
Mar 28 at 22:06
You may get TypeErrors if numBgn can't be parsed, surround it by try/catch and handle it accordingly
– geckos
Mar 28 at 22:08
@feliks No, then the error changes to TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'
– anythingButThat
Mar 29 at 18:25
@geckos That gives TypeError: int() argument must be a string, a bytes-like object or a number, not 'Cell'
– anythingButThat
Mar 29 at 18:28
Does type casting work? I.e. converting
sourceIdDict.get(src)
to int(sourceIdDict.get(src))
?– feliks
Mar 28 at 22:03
Does type casting work? I.e. converting
sourceIdDict.get(src)
to int(sourceIdDict.get(src))
?– feliks
Mar 28 at 22:03
You may be getting a non-integer from the dict or a None if the key isn't present, it depends on the input. I think you should replace the last line of first for by
sourceIdDict[source]=int(numBgn)
– geckos
Mar 28 at 22:06
You may be getting a non-integer from the dict or a None if the key isn't present, it depends on the input. I think you should replace the last line of first for by
sourceIdDict[source]=int(numBgn)
– geckos
Mar 28 at 22:06
You may get TypeErrors if numBgn can't be parsed, surround it by try/catch and handle it accordingly
– geckos
Mar 28 at 22:08
You may get TypeErrors if numBgn can't be parsed, surround it by try/catch and handle it accordingly
– geckos
Mar 28 at 22:08
@feliks No, then the error changes to TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'
– anythingButThat
Mar 29 at 18:25
@feliks No, then the error changes to TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'
– anythingButThat
Mar 29 at 18:25
@geckos That gives TypeError: int() argument must be a string, a bytes-like object or a number, not 'Cell'
– anythingButThat
Mar 29 at 18:28
@geckos That gives TypeError: int() argument must be a string, a bytes-like object or a number, not 'Cell'
– anythingButThat
Mar 29 at 18:28
|
show 2 more comments
0
active
oldest
votes
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/4.0/"u003ecc by-sa 4.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%2f55407494%2fuse-dictionary-value-to-parse-a-string-in-python%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
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%2f55407494%2fuse-dictionary-value-to-parse-a-string-in-python%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
Does type casting work? I.e. converting
sourceIdDict.get(src)
toint(sourceIdDict.get(src))
?– feliks
Mar 28 at 22:03
You may be getting a non-integer from the dict or a None if the key isn't present, it depends on the input. I think you should replace the last line of first for by
sourceIdDict[source]=int(numBgn)
– geckos
Mar 28 at 22:06
You may get TypeErrors if numBgn can't be parsed, surround it by try/catch and handle it accordingly
– geckos
Mar 28 at 22:08
@feliks No, then the error changes to TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'
– anythingButThat
Mar 29 at 18:25
@geckos That gives TypeError: int() argument must be a string, a bytes-like object or a number, not 'Cell'
– anythingButThat
Mar 29 at 18:28