python print error

The name of the pictureThe name of the pictureThe name of the pictureClash 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.










share|improve this question













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.line element1 element2 element3 2.line element1 (space or blabla) element3 3.line element1 element2 element3
    – tioma
    Aug 23 at 22:10















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.










share|improve this question













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.line element1 element2 element3 2.line element1 (space or blabla) element3 3.line element1 element2 element3
    – tioma
    Aug 23 at 22:10













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.










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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.line element1 element2 element3 2.line element1 (space or blabla) element3 3.line element1 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











  • 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
















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













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.






share|improve this answer



























    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.






    share|improve this answer




















    • 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










    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',
    convertImagesToLinks: true,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    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%2f51994881%2fpython-print-error%23new-answer', 'question_page');

    );

    Post as a guest






























    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.






    share|improve this answer
























      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.






      share|improve this answer






















        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.






        share|improve this answer












        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Aug 23 at 22:10









        Kevin He

        1608




        1608






















            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.






            share|improve this answer




















            • 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














            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.






            share|improve this answer




















            • 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












            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.






            share|improve this answer












            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.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            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
















            • 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

















             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            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













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Displaying single band from multi-band raster using QGIS

            How many registers does an x86_64 CPU actually have?