python print error
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
sample.json
"datalist": [
"element1": "content",
"element2": "content",
"element3": "content"
,
"element1": "content",
"element2": "content",
"element3": "content"
,
"element1": "content",
"element2": "content",
"element3": "content"
]
Python Code
import json
file = "/home/sample.json"
with open(file) as f:
data = json.load(f)
for elem in data["datalist"]:
print(elem["element1"] + ' \ ' + elem["element2"] + ' \ ' + elem["element3"])
Good Work Print Result : element1 element2 element3
But, another sample.json
"datalist": [
"element1": "content",
"element2": "content",
"element3": "content"
,
"element1": "content",
"element3": "content"
,
"element1": "content",
"element2": "content",
"element3": "content"
]
here in second section ; element2 does not exist
if element2 does not exist, it gives KeyError: 'element2'
for elem in data["datalist"]:
print(elem["element1"] + ' \ ' + elem["element2"] + ' \ ' + elem["element3"])
printing first section but when it comes second section stop and KeyError: 'element2'
how can fix it ? thx.
python json
migrated from unix.stackexchange.com Aug 23 at 22:02
This question came from our site for users of Linux, FreeBSD and other Un*x-like operating systems.
add a comment |Â
up vote
0
down vote
favorite
sample.json
"datalist": [
"element1": "content",
"element2": "content",
"element3": "content"
,
"element1": "content",
"element2": "content",
"element3": "content"
,
"element1": "content",
"element2": "content",
"element3": "content"
]
Python Code
import json
file = "/home/sample.json"
with open(file) as f:
data = json.load(f)
for elem in data["datalist"]:
print(elem["element1"] + ' \ ' + elem["element2"] + ' \ ' + elem["element3"])
Good Work Print Result : element1 element2 element3
But, another sample.json
"datalist": [
"element1": "content",
"element2": "content",
"element3": "content"
,
"element1": "content",
"element3": "content"
,
"element1": "content",
"element2": "content",
"element3": "content"
]
here in second section ; element2 does not exist
if element2 does not exist, it gives KeyError: 'element2'
for elem in data["datalist"]:
print(elem["element1"] + ' \ ' + elem["element2"] + ' \ ' + elem["element3"])
printing first section but when it comes second section stop and KeyError: 'element2'
how can fix it ? thx.
python json
migrated from unix.stackexchange.com Aug 23 at 22:02
This question came from our site for users of Linux, FreeBSD and other Un*x-like operating systems.
You can use some if statement checking the length. If the length is 2, you can print element one and element 3. Else print all three elements
â Bazingaa
Aug 23 at 22:08
thx, how to? @Bazingaa i want result : 1.lineelement1 element2 element3
2.lineelement1 (space or blabla) element3
3.lineelement1 element2 element3
â tioma
Aug 23 at 22:10
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
sample.json
"datalist": [
"element1": "content",
"element2": "content",
"element3": "content"
,
"element1": "content",
"element2": "content",
"element3": "content"
,
"element1": "content",
"element2": "content",
"element3": "content"
]
Python Code
import json
file = "/home/sample.json"
with open(file) as f:
data = json.load(f)
for elem in data["datalist"]:
print(elem["element1"] + ' \ ' + elem["element2"] + ' \ ' + elem["element3"])
Good Work Print Result : element1 element2 element3
But, another sample.json
"datalist": [
"element1": "content",
"element2": "content",
"element3": "content"
,
"element1": "content",
"element3": "content"
,
"element1": "content",
"element2": "content",
"element3": "content"
]
here in second section ; element2 does not exist
if element2 does not exist, it gives KeyError: 'element2'
for elem in data["datalist"]:
print(elem["element1"] + ' \ ' + elem["element2"] + ' \ ' + elem["element3"])
printing first section but when it comes second section stop and KeyError: 'element2'
how can fix it ? thx.
python json
sample.json
"datalist": [
"element1": "content",
"element2": "content",
"element3": "content"
,
"element1": "content",
"element2": "content",
"element3": "content"
,
"element1": "content",
"element2": "content",
"element3": "content"
]
Python Code
import json
file = "/home/sample.json"
with open(file) as f:
data = json.load(f)
for elem in data["datalist"]:
print(elem["element1"] + ' \ ' + elem["element2"] + ' \ ' + elem["element3"])
Good Work Print Result : element1 element2 element3
But, another sample.json
"datalist": [
"element1": "content",
"element2": "content",
"element3": "content"
,
"element1": "content",
"element3": "content"
,
"element1": "content",
"element2": "content",
"element3": "content"
]
here in second section ; element2 does not exist
if element2 does not exist, it gives KeyError: 'element2'
for elem in data["datalist"]:
print(elem["element1"] + ' \ ' + elem["element2"] + ' \ ' + elem["element3"])
printing first section but when it comes second section stop and KeyError: 'element2'
how can fix it ? thx.
python json
python json
asked Aug 23 at 21:14
tioma
82
82
migrated from unix.stackexchange.com Aug 23 at 22:02
This question came from our site for users of Linux, FreeBSD and other Un*x-like operating systems.
migrated from unix.stackexchange.com Aug 23 at 22:02
This question came from our site for users of Linux, FreeBSD and other Un*x-like operating systems.
You can use some if statement checking the length. If the length is 2, you can print element one and element 3. Else print all three elements
â Bazingaa
Aug 23 at 22:08
thx, how to? @Bazingaa i want result : 1.lineelement1 element2 element3
2.lineelement1 (space or blabla) element3
3.lineelement1 element2 element3
â tioma
Aug 23 at 22:10
add a comment |Â
You can use some if statement checking the length. If the length is 2, you can print element one and element 3. Else print all three elements
â Bazingaa
Aug 23 at 22:08
thx, how to? @Bazingaa i want result : 1.lineelement1 element2 element3
2.lineelement1 (space or blabla) element3
3.lineelement1 element2 element3
â tioma
Aug 23 at 22:10
You can use some if statement checking the length. If the length is 2, you can print element one and element 3. Else print all three elements
â Bazingaa
Aug 23 at 22:08
You can use some if statement checking the length. If the length is 2, you can print element one and element 3. Else print all three elements
â Bazingaa
Aug 23 at 22:08
thx, how to? @Bazingaa i want result : 1.line
element1 element2 element3
2.line element1 (space or blabla) element3
3.line element1 element2 element3
â tioma
Aug 23 at 22:10
thx, how to? @Bazingaa i want result : 1.line
element1 element2 element3
2.line element1 (space or blabla) element3
3.line element1 element2 element3
â tioma
Aug 23 at 22:10
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
Python dictionaries have a get
method that allows you to return a default value if the key is not found. For example, if d = 1: 2, 3: 4 ; v = 10
, d.get(1, v)
gives 2
, d.get(5, v)
gives 10
.
So this case you can just give a default value to your code such as elem.get('element2', '[element2 missing]')
to signify a missing element in the print outputs and the code will run error-free.
add a comment |Â
up vote
0
down vote
There are multiple options:
- Fix the program, to tolerate missing data
- Fix the data, to be parse-able by the program
Fixing the program
Instead of:
print(elem["element1"] + ' \ ' + elem["element2"] + ' \ ' + elem["element3"])
you need to check whether the elements are there:
if "element1" in elem.keys() and "element2" in elem.keys() and "element3" in
elem.keys():
print(elem["element1"] + ' \ ' ...
Fixing the data
Of course, this is easiest: add a key and a value for "element2" in the second dictionary from datalist.
I worked but it was incomplete and not what I wanted :( ignoring all element2 ? if there is no element2, i want to leave it space (" ") instead or write "blabla"
â tioma
Aug 23 at 21:43
i want result :element1 element2 element3
element1 (space or blabla) element3
element1 element2 element3
â tioma
Aug 23 at 21:56
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Python dictionaries have a get
method that allows you to return a default value if the key is not found. For example, if d = 1: 2, 3: 4 ; v = 10
, d.get(1, v)
gives 2
, d.get(5, v)
gives 10
.
So this case you can just give a default value to your code such as elem.get('element2', '[element2 missing]')
to signify a missing element in the print outputs and the code will run error-free.
add a comment |Â
up vote
1
down vote
accepted
Python dictionaries have a get
method that allows you to return a default value if the key is not found. For example, if d = 1: 2, 3: 4 ; v = 10
, d.get(1, v)
gives 2
, d.get(5, v)
gives 10
.
So this case you can just give a default value to your code such as elem.get('element2', '[element2 missing]')
to signify a missing element in the print outputs and the code will run error-free.
add a comment |Â
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Python dictionaries have a get
method that allows you to return a default value if the key is not found. For example, if d = 1: 2, 3: 4 ; v = 10
, d.get(1, v)
gives 2
, d.get(5, v)
gives 10
.
So this case you can just give a default value to your code such as elem.get('element2', '[element2 missing]')
to signify a missing element in the print outputs and the code will run error-free.
Python dictionaries have a get
method that allows you to return a default value if the key is not found. For example, if d = 1: 2, 3: 4 ; v = 10
, d.get(1, v)
gives 2
, d.get(5, v)
gives 10
.
So this case you can just give a default value to your code such as elem.get('element2', '[element2 missing]')
to signify a missing element in the print outputs and the code will run error-free.
answered Aug 23 at 22:10
Kevin He
1608
1608
add a comment |Â
add a comment |Â
up vote
0
down vote
There are multiple options:
- Fix the program, to tolerate missing data
- Fix the data, to be parse-able by the program
Fixing the program
Instead of:
print(elem["element1"] + ' \ ' + elem["element2"] + ' \ ' + elem["element3"])
you need to check whether the elements are there:
if "element1" in elem.keys() and "element2" in elem.keys() and "element3" in
elem.keys():
print(elem["element1"] + ' \ ' ...
Fixing the data
Of course, this is easiest: add a key and a value for "element2" in the second dictionary from datalist.
I worked but it was incomplete and not what I wanted :( ignoring all element2 ? if there is no element2, i want to leave it space (" ") instead or write "blabla"
â tioma
Aug 23 at 21:43
i want result :element1 element2 element3
element1 (space or blabla) element3
element1 element2 element3
â tioma
Aug 23 at 21:56
add a comment |Â
up vote
0
down vote
There are multiple options:
- Fix the program, to tolerate missing data
- Fix the data, to be parse-able by the program
Fixing the program
Instead of:
print(elem["element1"] + ' \ ' + elem["element2"] + ' \ ' + elem["element3"])
you need to check whether the elements are there:
if "element1" in elem.keys() and "element2" in elem.keys() and "element3" in
elem.keys():
print(elem["element1"] + ' \ ' ...
Fixing the data
Of course, this is easiest: add a key and a value for "element2" in the second dictionary from datalist.
I worked but it was incomplete and not what I wanted :( ignoring all element2 ? if there is no element2, i want to leave it space (" ") instead or write "blabla"
â tioma
Aug 23 at 21:43
i want result :element1 element2 element3
element1 (space or blabla) element3
element1 element2 element3
â tioma
Aug 23 at 21:56
add a comment |Â
up vote
0
down vote
up vote
0
down vote
There are multiple options:
- Fix the program, to tolerate missing data
- Fix the data, to be parse-able by the program
Fixing the program
Instead of:
print(elem["element1"] + ' \ ' + elem["element2"] + ' \ ' + elem["element3"])
you need to check whether the elements are there:
if "element1" in elem.keys() and "element2" in elem.keys() and "element3" in
elem.keys():
print(elem["element1"] + ' \ ' ...
Fixing the data
Of course, this is easiest: add a key and a value for "element2" in the second dictionary from datalist.
There are multiple options:
- Fix the program, to tolerate missing data
- Fix the data, to be parse-able by the program
Fixing the program
Instead of:
print(elem["element1"] + ' \ ' + elem["element2"] + ' \ ' + elem["element3"])
you need to check whether the elements are there:
if "element1" in elem.keys() and "element2" in elem.keys() and "element3" in
elem.keys():
print(elem["element1"] + ' \ ' ...
Fixing the data
Of course, this is easiest: add a key and a value for "element2" in the second dictionary from datalist.
answered Aug 23 at 21:30
danuker
17513
17513
I worked but it was incomplete and not what I wanted :( ignoring all element2 ? if there is no element2, i want to leave it space (" ") instead or write "blabla"
â tioma
Aug 23 at 21:43
i want result :element1 element2 element3
element1 (space or blabla) element3
element1 element2 element3
â tioma
Aug 23 at 21:56
add a comment |Â
I worked but it was incomplete and not what I wanted :( ignoring all element2 ? if there is no element2, i want to leave it space (" ") instead or write "blabla"
â tioma
Aug 23 at 21:43
i want result :element1 element2 element3
element1 (space or blabla) element3
element1 element2 element3
â tioma
Aug 23 at 21:56
I worked but it was incomplete and not what I wanted :( ignoring all element2 ? if there is no element2, i want to leave it space (" ") instead or write "blabla"
â tioma
Aug 23 at 21:43
I worked but it was incomplete and not what I wanted :( ignoring all element2 ? if there is no element2, i want to leave it space (" ") instead or write "blabla"
â tioma
Aug 23 at 21:43
i want result :
element1 element2 element3
element1 (space or blabla) element3
element1 element2 element3
â tioma
Aug 23 at 21:56
i want result :
element1 element2 element3
element1 (space or blabla) element3
element1 element2 element3
â tioma
Aug 23 at 21:56
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51994881%2fpython-print-error%23new-answer', 'question_page');
);
Post as a guest
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
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
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
You can use some if statement checking the length. If the length is 2, you can print element one and element 3. Else print all three elements
â Bazingaa
Aug 23 at 22:08
thx, how to? @Bazingaa i want result : 1.line
element1 element2 element3
2.lineelement1 (space or blabla) element3
3.lineelement1 element2 element3
â tioma
Aug 23 at 22:10