Case insensitive dictionary lookup in Python one-liner called from bash
I'm not finding a solution to this, even on the other topics here relating to case insensitive dictionaries in Python.
I have a bash script, and I need to look up a value from a .json file, using a key extracted from another .json file. (These files are part of Chrome extensions.) The easiest way I've found to do this is by putting Python one-liners in the bash script. Here's what I've got right now:
extName=$(cat "$localePath" | python -c "import sys, json; sys.stdout.write(json.load(sys.stdin)['"$nameKey"']['message'])")
Here's the problem... Chrome doesn't seem to care about case in its json files, and in some extensions, the case of the key that I've previously extracted (from the extension's manifest.json) into $nameKey does not match the case of the key in $localePath (a path to a messages.json file in one of the extension's _locales folders).
Is there a way to do this in a Python one-liner, or am I going to have to find a different way to do this?
python bash
add a comment |
I'm not finding a solution to this, even on the other topics here relating to case insensitive dictionaries in Python.
I have a bash script, and I need to look up a value from a .json file, using a key extracted from another .json file. (These files are part of Chrome extensions.) The easiest way I've found to do this is by putting Python one-liners in the bash script. Here's what I've got right now:
extName=$(cat "$localePath" | python -c "import sys, json; sys.stdout.write(json.load(sys.stdin)['"$nameKey"']['message'])")
Here's the problem... Chrome doesn't seem to care about case in its json files, and in some extensions, the case of the key that I've previously extracted (from the extension's manifest.json) into $nameKey does not match the case of the key in $localePath (a path to a messages.json file in one of the extension's _locales folders).
Is there a way to do this in a Python one-liner, or am I going to have to find a different way to do this?
python bash
Can there be mixed case keys like'KeY'or is it either the content of$nameKeyor$nameKeyin all lower case?
– timgeb
Nov 27 '18 at 20:51
Unrelated, but it is safer to passnameKeyas an argument rather than trying to interpolate it into the Python script.python -c "...; write(load(stdin)[argv[1]]['message'])" "$nameKey" < "$localePath".
– chepner
Nov 27 '18 at 21:24
The nameKey value is taken straight from a Chrome extension, so it's unlikely that it would be a dangerous value, but point taken. However, that syntax doesn't seem to work. I get error messages that "name 'argv' is not defined".
– T. Reed
Nov 28 '18 at 18:59
add a comment |
I'm not finding a solution to this, even on the other topics here relating to case insensitive dictionaries in Python.
I have a bash script, and I need to look up a value from a .json file, using a key extracted from another .json file. (These files are part of Chrome extensions.) The easiest way I've found to do this is by putting Python one-liners in the bash script. Here's what I've got right now:
extName=$(cat "$localePath" | python -c "import sys, json; sys.stdout.write(json.load(sys.stdin)['"$nameKey"']['message'])")
Here's the problem... Chrome doesn't seem to care about case in its json files, and in some extensions, the case of the key that I've previously extracted (from the extension's manifest.json) into $nameKey does not match the case of the key in $localePath (a path to a messages.json file in one of the extension's _locales folders).
Is there a way to do this in a Python one-liner, or am I going to have to find a different way to do this?
python bash
I'm not finding a solution to this, even on the other topics here relating to case insensitive dictionaries in Python.
I have a bash script, and I need to look up a value from a .json file, using a key extracted from another .json file. (These files are part of Chrome extensions.) The easiest way I've found to do this is by putting Python one-liners in the bash script. Here's what I've got right now:
extName=$(cat "$localePath" | python -c "import sys, json; sys.stdout.write(json.load(sys.stdin)['"$nameKey"']['message'])")
Here's the problem... Chrome doesn't seem to care about case in its json files, and in some extensions, the case of the key that I've previously extracted (from the extension's manifest.json) into $nameKey does not match the case of the key in $localePath (a path to a messages.json file in one of the extension's _locales folders).
Is there a way to do this in a Python one-liner, or am I going to have to find a different way to do this?
python bash
python bash
asked Nov 27 '18 at 20:46
T. ReedT. Reed
395
395
Can there be mixed case keys like'KeY'or is it either the content of$nameKeyor$nameKeyin all lower case?
– timgeb
Nov 27 '18 at 20:51
Unrelated, but it is safer to passnameKeyas an argument rather than trying to interpolate it into the Python script.python -c "...; write(load(stdin)[argv[1]]['message'])" "$nameKey" < "$localePath".
– chepner
Nov 27 '18 at 21:24
The nameKey value is taken straight from a Chrome extension, so it's unlikely that it would be a dangerous value, but point taken. However, that syntax doesn't seem to work. I get error messages that "name 'argv' is not defined".
– T. Reed
Nov 28 '18 at 18:59
add a comment |
Can there be mixed case keys like'KeY'or is it either the content of$nameKeyor$nameKeyin all lower case?
– timgeb
Nov 27 '18 at 20:51
Unrelated, but it is safer to passnameKeyas an argument rather than trying to interpolate it into the Python script.python -c "...; write(load(stdin)[argv[1]]['message'])" "$nameKey" < "$localePath".
– chepner
Nov 27 '18 at 21:24
The nameKey value is taken straight from a Chrome extension, so it's unlikely that it would be a dangerous value, but point taken. However, that syntax doesn't seem to work. I get error messages that "name 'argv' is not defined".
– T. Reed
Nov 28 '18 at 18:59
Can there be mixed case keys like
'KeY' or is it either the content of $nameKey or $nameKey in all lower case?– timgeb
Nov 27 '18 at 20:51
Can there be mixed case keys like
'KeY' or is it either the content of $nameKey or $nameKey in all lower case?– timgeb
Nov 27 '18 at 20:51
Unrelated, but it is safer to pass
nameKey as an argument rather than trying to interpolate it into the Python script. python -c "...; write(load(stdin)[argv[1]]['message'])" "$nameKey" < "$localePath".– chepner
Nov 27 '18 at 21:24
Unrelated, but it is safer to pass
nameKey as an argument rather than trying to interpolate it into the Python script. python -c "...; write(load(stdin)[argv[1]]['message'])" "$nameKey" < "$localePath".– chepner
Nov 27 '18 at 21:24
The nameKey value is taken straight from a Chrome extension, so it's unlikely that it would be a dangerous value, but point taken. However, that syntax doesn't seem to work. I get error messages that "name 'argv' is not defined".
– T. Reed
Nov 28 '18 at 18:59
The nameKey value is taken straight from a Chrome extension, so it's unlikely that it would be a dangerous value, but point taken. However, that syntax doesn't seem to work. I get error messages that "name 'argv' is not defined".
– T. Reed
Nov 28 '18 at 18:59
add a comment |
2 Answers
2
active
oldest
votes
It's not the most elegant one-liner ever, but you can transform all your dict keys and values to lower-case entries and to consult it using a lower case key as well.
Python 2.x:
extName=$(cat "$localePath" | python -c "import sys, json; {key.lower(): value.lower() for key, value in sys.stdout.write(json.load(sys.stdin).iteitems()}['"$nameKey"'.lower()]['message'])")
Python 3.x:
extName=$(cat "$localePath" | python -c "import sys, json; {key.lower(): value.lower() for key, value in sys.stdout.write(json.load(sys.stdin).items()}['"$nameKey"'.lower()]['message'])")
That generates a syntax error, with a caret pointing at the closing '}': SyntaxError: invalid syntax File "<string>", line 1 import sys, json; {key.lower(): value.lower() for key, value in sys.stdout.write(json.load(sys.stdin).iteitems()}['APP_NAME'.lower()]['message'])
– T. Reed
Nov 28 '18 at 18:34
I've corrected the syntax errors in the Python 2.x line, but I still get an error that I don't understand, as all the objects I'm calling lower() on should be strings, not dicts. Traceback (most recent call last): File "<string>", line 1, in <module> File "<string>", line 1, in <dictcomp> AttributeError: 'dict' object has no attribute 'lower'
– T. Reed
Nov 28 '18 at 18:47
Sorry, my answer was wrong, I assumed that you had a dict of strings which is not your case. Can you give an example of your dict content?
– Aurora Wang
Nov 28 '18 at 19:55
add a comment |
I got it! I fixed some of the issues in Aurora's suggested solution for Python 2.x, and ended up with this, which works:
extName=$(cat "$localePath" | python -c "import sys, json; sys.stdout.write(dict((key.lower(), value) for key, value in json.load(sys.stdin).iteritems())['""$nameKey""'.lower()]['message'])")
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%2f53507873%2fcase-insensitive-dictionary-lookup-in-python-one-liner-called-from-bash%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
It's not the most elegant one-liner ever, but you can transform all your dict keys and values to lower-case entries and to consult it using a lower case key as well.
Python 2.x:
extName=$(cat "$localePath" | python -c "import sys, json; {key.lower(): value.lower() for key, value in sys.stdout.write(json.load(sys.stdin).iteitems()}['"$nameKey"'.lower()]['message'])")
Python 3.x:
extName=$(cat "$localePath" | python -c "import sys, json; {key.lower(): value.lower() for key, value in sys.stdout.write(json.load(sys.stdin).items()}['"$nameKey"'.lower()]['message'])")
That generates a syntax error, with a caret pointing at the closing '}': SyntaxError: invalid syntax File "<string>", line 1 import sys, json; {key.lower(): value.lower() for key, value in sys.stdout.write(json.load(sys.stdin).iteitems()}['APP_NAME'.lower()]['message'])
– T. Reed
Nov 28 '18 at 18:34
I've corrected the syntax errors in the Python 2.x line, but I still get an error that I don't understand, as all the objects I'm calling lower() on should be strings, not dicts. Traceback (most recent call last): File "<string>", line 1, in <module> File "<string>", line 1, in <dictcomp> AttributeError: 'dict' object has no attribute 'lower'
– T. Reed
Nov 28 '18 at 18:47
Sorry, my answer was wrong, I assumed that you had a dict of strings which is not your case. Can you give an example of your dict content?
– Aurora Wang
Nov 28 '18 at 19:55
add a comment |
It's not the most elegant one-liner ever, but you can transform all your dict keys and values to lower-case entries and to consult it using a lower case key as well.
Python 2.x:
extName=$(cat "$localePath" | python -c "import sys, json; {key.lower(): value.lower() for key, value in sys.stdout.write(json.load(sys.stdin).iteitems()}['"$nameKey"'.lower()]['message'])")
Python 3.x:
extName=$(cat "$localePath" | python -c "import sys, json; {key.lower(): value.lower() for key, value in sys.stdout.write(json.load(sys.stdin).items()}['"$nameKey"'.lower()]['message'])")
That generates a syntax error, with a caret pointing at the closing '}': SyntaxError: invalid syntax File "<string>", line 1 import sys, json; {key.lower(): value.lower() for key, value in sys.stdout.write(json.load(sys.stdin).iteitems()}['APP_NAME'.lower()]['message'])
– T. Reed
Nov 28 '18 at 18:34
I've corrected the syntax errors in the Python 2.x line, but I still get an error that I don't understand, as all the objects I'm calling lower() on should be strings, not dicts. Traceback (most recent call last): File "<string>", line 1, in <module> File "<string>", line 1, in <dictcomp> AttributeError: 'dict' object has no attribute 'lower'
– T. Reed
Nov 28 '18 at 18:47
Sorry, my answer was wrong, I assumed that you had a dict of strings which is not your case. Can you give an example of your dict content?
– Aurora Wang
Nov 28 '18 at 19:55
add a comment |
It's not the most elegant one-liner ever, but you can transform all your dict keys and values to lower-case entries and to consult it using a lower case key as well.
Python 2.x:
extName=$(cat "$localePath" | python -c "import sys, json; {key.lower(): value.lower() for key, value in sys.stdout.write(json.load(sys.stdin).iteitems()}['"$nameKey"'.lower()]['message'])")
Python 3.x:
extName=$(cat "$localePath" | python -c "import sys, json; {key.lower(): value.lower() for key, value in sys.stdout.write(json.load(sys.stdin).items()}['"$nameKey"'.lower()]['message'])")
It's not the most elegant one-liner ever, but you can transform all your dict keys and values to lower-case entries and to consult it using a lower case key as well.
Python 2.x:
extName=$(cat "$localePath" | python -c "import sys, json; {key.lower(): value.lower() for key, value in sys.stdout.write(json.load(sys.stdin).iteitems()}['"$nameKey"'.lower()]['message'])")
Python 3.x:
extName=$(cat "$localePath" | python -c "import sys, json; {key.lower(): value.lower() for key, value in sys.stdout.write(json.load(sys.stdin).items()}['"$nameKey"'.lower()]['message'])")
answered Nov 27 '18 at 21:01
Aurora WangAurora Wang
733316
733316
That generates a syntax error, with a caret pointing at the closing '}': SyntaxError: invalid syntax File "<string>", line 1 import sys, json; {key.lower(): value.lower() for key, value in sys.stdout.write(json.load(sys.stdin).iteitems()}['APP_NAME'.lower()]['message'])
– T. Reed
Nov 28 '18 at 18:34
I've corrected the syntax errors in the Python 2.x line, but I still get an error that I don't understand, as all the objects I'm calling lower() on should be strings, not dicts. Traceback (most recent call last): File "<string>", line 1, in <module> File "<string>", line 1, in <dictcomp> AttributeError: 'dict' object has no attribute 'lower'
– T. Reed
Nov 28 '18 at 18:47
Sorry, my answer was wrong, I assumed that you had a dict of strings which is not your case. Can you give an example of your dict content?
– Aurora Wang
Nov 28 '18 at 19:55
add a comment |
That generates a syntax error, with a caret pointing at the closing '}': SyntaxError: invalid syntax File "<string>", line 1 import sys, json; {key.lower(): value.lower() for key, value in sys.stdout.write(json.load(sys.stdin).iteitems()}['APP_NAME'.lower()]['message'])
– T. Reed
Nov 28 '18 at 18:34
I've corrected the syntax errors in the Python 2.x line, but I still get an error that I don't understand, as all the objects I'm calling lower() on should be strings, not dicts. Traceback (most recent call last): File "<string>", line 1, in <module> File "<string>", line 1, in <dictcomp> AttributeError: 'dict' object has no attribute 'lower'
– T. Reed
Nov 28 '18 at 18:47
Sorry, my answer was wrong, I assumed that you had a dict of strings which is not your case. Can you give an example of your dict content?
– Aurora Wang
Nov 28 '18 at 19:55
That generates a syntax error, with a caret pointing at the closing '}': SyntaxError: invalid syntax File "<string>", line 1 import sys, json; {key.lower(): value.lower() for key, value in sys.stdout.write(json.load(sys.stdin).iteitems()}['APP_NAME'.lower()]['message'])
– T. Reed
Nov 28 '18 at 18:34
That generates a syntax error, with a caret pointing at the closing '}': SyntaxError: invalid syntax File "<string>", line 1 import sys, json; {key.lower(): value.lower() for key, value in sys.stdout.write(json.load(sys.stdin).iteitems()}['APP_NAME'.lower()]['message'])
– T. Reed
Nov 28 '18 at 18:34
I've corrected the syntax errors in the Python 2.x line, but I still get an error that I don't understand, as all the objects I'm calling lower() on should be strings, not dicts. Traceback (most recent call last): File "<string>", line 1, in <module> File "<string>", line 1, in <dictcomp> AttributeError: 'dict' object has no attribute 'lower'
– T. Reed
Nov 28 '18 at 18:47
I've corrected the syntax errors in the Python 2.x line, but I still get an error that I don't understand, as all the objects I'm calling lower() on should be strings, not dicts. Traceback (most recent call last): File "<string>", line 1, in <module> File "<string>", line 1, in <dictcomp> AttributeError: 'dict' object has no attribute 'lower'
– T. Reed
Nov 28 '18 at 18:47
Sorry, my answer was wrong, I assumed that you had a dict of strings which is not your case. Can you give an example of your dict content?
– Aurora Wang
Nov 28 '18 at 19:55
Sorry, my answer was wrong, I assumed that you had a dict of strings which is not your case. Can you give an example of your dict content?
– Aurora Wang
Nov 28 '18 at 19:55
add a comment |
I got it! I fixed some of the issues in Aurora's suggested solution for Python 2.x, and ended up with this, which works:
extName=$(cat "$localePath" | python -c "import sys, json; sys.stdout.write(dict((key.lower(), value) for key, value in json.load(sys.stdin).iteritems())['""$nameKey""'.lower()]['message'])")
add a comment |
I got it! I fixed some of the issues in Aurora's suggested solution for Python 2.x, and ended up with this, which works:
extName=$(cat "$localePath" | python -c "import sys, json; sys.stdout.write(dict((key.lower(), value) for key, value in json.load(sys.stdin).iteritems())['""$nameKey""'.lower()]['message'])")
add a comment |
I got it! I fixed some of the issues in Aurora's suggested solution for Python 2.x, and ended up with this, which works:
extName=$(cat "$localePath" | python -c "import sys, json; sys.stdout.write(dict((key.lower(), value) for key, value in json.load(sys.stdin).iteritems())['""$nameKey""'.lower()]['message'])")
I got it! I fixed some of the issues in Aurora's suggested solution for Python 2.x, and ended up with this, which works:
extName=$(cat "$localePath" | python -c "import sys, json; sys.stdout.write(dict((key.lower(), value) for key, value in json.load(sys.stdin).iteritems())['""$nameKey""'.lower()]['message'])")
answered Nov 28 '18 at 18:54
T. ReedT. Reed
395
395
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.
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%2f53507873%2fcase-insensitive-dictionary-lookup-in-python-one-liner-called-from-bash%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
Can there be mixed case keys like
'KeY'or is it either the content of$nameKeyor$nameKeyin all lower case?– timgeb
Nov 27 '18 at 20:51
Unrelated, but it is safer to pass
nameKeyas an argument rather than trying to interpolate it into the Python script.python -c "...; write(load(stdin)[argv[1]]['message'])" "$nameKey" < "$localePath".– chepner
Nov 27 '18 at 21:24
The nameKey value is taken straight from a Chrome extension, so it's unlikely that it would be a dangerous value, but point taken. However, that syntax doesn't seem to work. I get error messages that "name 'argv' is not defined".
– T. Reed
Nov 28 '18 at 18:59