Encode decode problem while writing to excell worksheet in django view





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I get below unicodedecodeerror while trying to write to excell worksheet.




Exception Type: UnicodeDecodeError
Exception Value:

'ascii' codec can't decode byte 0xc3 in position 7: ordinal not in range(128) The string that could not be encoded/decoded was: i>����R<




My view lines :



def file_write(input):
handle1=open('/tmp/filelog.txt','a')
handle1.write(str(input))
handle1.close()

workbook = xlsxwriter.Workbook('report.xlsx')
worksheet = workbook.add_worksheet()

teachertitle = "ÖĞR"
file_write(teachertitle)
worksheet.write("A4", teachertitle, titlescell)
workbook.close()


The strange thing is. File_write function is working well and it writes "ÖĞR" to a local text file. But when i try to write "ÖĞR" to excell workseeht it throws error.



I also tried worksheet.write("A4", teachertitle.encode('utf-8'), titlescell) but still problem continue.



I also have # -- coding: utf-8 -- at the beginning of views.py










share|improve this question


















  • 1





    Are you using python3 or python2? Also, can you please post the full trace to your exception?

    – 2ps
    Nov 23 '18 at 19:39











  • The source file encoding should actually be specified with: # -*- coding: utf-8 -*- (note the asterisks).

    – Will Keeling
    Nov 26 '18 at 10:09











  • right the original code is with *

    – ivbtar
    Nov 29 '18 at 14:23


















0















I get below unicodedecodeerror while trying to write to excell worksheet.




Exception Type: UnicodeDecodeError
Exception Value:

'ascii' codec can't decode byte 0xc3 in position 7: ordinal not in range(128) The string that could not be encoded/decoded was: i>����R<




My view lines :



def file_write(input):
handle1=open('/tmp/filelog.txt','a')
handle1.write(str(input))
handle1.close()

workbook = xlsxwriter.Workbook('report.xlsx')
worksheet = workbook.add_worksheet()

teachertitle = "ÖĞR"
file_write(teachertitle)
worksheet.write("A4", teachertitle, titlescell)
workbook.close()


The strange thing is. File_write function is working well and it writes "ÖĞR" to a local text file. But when i try to write "ÖĞR" to excell workseeht it throws error.



I also tried worksheet.write("A4", teachertitle.encode('utf-8'), titlescell) but still problem continue.



I also have # -- coding: utf-8 -- at the beginning of views.py










share|improve this question


















  • 1





    Are you using python3 or python2? Also, can you please post the full trace to your exception?

    – 2ps
    Nov 23 '18 at 19:39











  • The source file encoding should actually be specified with: # -*- coding: utf-8 -*- (note the asterisks).

    – Will Keeling
    Nov 26 '18 at 10:09











  • right the original code is with *

    – ivbtar
    Nov 29 '18 at 14:23














0












0








0








I get below unicodedecodeerror while trying to write to excell worksheet.




Exception Type: UnicodeDecodeError
Exception Value:

'ascii' codec can't decode byte 0xc3 in position 7: ordinal not in range(128) The string that could not be encoded/decoded was: i>����R<




My view lines :



def file_write(input):
handle1=open('/tmp/filelog.txt','a')
handle1.write(str(input))
handle1.close()

workbook = xlsxwriter.Workbook('report.xlsx')
worksheet = workbook.add_worksheet()

teachertitle = "ÖĞR"
file_write(teachertitle)
worksheet.write("A4", teachertitle, titlescell)
workbook.close()


The strange thing is. File_write function is working well and it writes "ÖĞR" to a local text file. But when i try to write "ÖĞR" to excell workseeht it throws error.



I also tried worksheet.write("A4", teachertitle.encode('utf-8'), titlescell) but still problem continue.



I also have # -- coding: utf-8 -- at the beginning of views.py










share|improve this question














I get below unicodedecodeerror while trying to write to excell worksheet.




Exception Type: UnicodeDecodeError
Exception Value:

'ascii' codec can't decode byte 0xc3 in position 7: ordinal not in range(128) The string that could not be encoded/decoded was: i>����R<




My view lines :



def file_write(input):
handle1=open('/tmp/filelog.txt','a')
handle1.write(str(input))
handle1.close()

workbook = xlsxwriter.Workbook('report.xlsx')
worksheet = workbook.add_worksheet()

teachertitle = "ÖĞR"
file_write(teachertitle)
worksheet.write("A4", teachertitle, titlescell)
workbook.close()


The strange thing is. File_write function is working well and it writes "ÖĞR" to a local text file. But when i try to write "ÖĞR" to excell workseeht it throws error.



I also tried worksheet.write("A4", teachertitle.encode('utf-8'), titlescell) but still problem continue.



I also have # -- coding: utf-8 -- at the beginning of views.py







python django character-encoding django-views django-excel






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 14:35









ivbtarivbtar

140110




140110








  • 1





    Are you using python3 or python2? Also, can you please post the full trace to your exception?

    – 2ps
    Nov 23 '18 at 19:39











  • The source file encoding should actually be specified with: # -*- coding: utf-8 -*- (note the asterisks).

    – Will Keeling
    Nov 26 '18 at 10:09











  • right the original code is with *

    – ivbtar
    Nov 29 '18 at 14:23














  • 1





    Are you using python3 or python2? Also, can you please post the full trace to your exception?

    – 2ps
    Nov 23 '18 at 19:39











  • The source file encoding should actually be specified with: # -*- coding: utf-8 -*- (note the asterisks).

    – Will Keeling
    Nov 26 '18 at 10:09











  • right the original code is with *

    – ivbtar
    Nov 29 '18 at 14:23








1




1





Are you using python3 or python2? Also, can you please post the full trace to your exception?

– 2ps
Nov 23 '18 at 19:39





Are you using python3 or python2? Also, can you please post the full trace to your exception?

– 2ps
Nov 23 '18 at 19:39













The source file encoding should actually be specified with: # -*- coding: utf-8 -*- (note the asterisks).

– Will Keeling
Nov 26 '18 at 10:09





The source file encoding should actually be specified with: # -*- coding: utf-8 -*- (note the asterisks).

– Will Keeling
Nov 26 '18 at 10:09













right the original code is with *

– ivbtar
Nov 29 '18 at 14:23





right the original code is with *

– ivbtar
Nov 29 '18 at 14:23












2 Answers
2






active

oldest

votes


















0














The problem is most likely with your file_write function, where you need to set the encoding of the file to be able to handle utf-8. In python3, you can do that using:




def file_write(input):
handle1=open('/tmp/filelog.txt','a', encoding='utf-8')
handle1.write(str(input))
handle1.close()





share|improve this answer
























  • file_write() is working well. Problem is with excell workbook

    – ivbtar
    Nov 29 '18 at 14:25



















0














At last,



Solution is :




worksheet.write("A4", teachertitle.decode('utf-8'), titlescell)




Decoding solved it. As i understand excell workbook needs the string to be decoded before writing to excell sheet.






share|improve this answer
























    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
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53448618%2fencode-decode-problem-while-writing-to-excell-worksheet-in-django-view%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









    0














    The problem is most likely with your file_write function, where you need to set the encoding of the file to be able to handle utf-8. In python3, you can do that using:




    def file_write(input):
    handle1=open('/tmp/filelog.txt','a', encoding='utf-8')
    handle1.write(str(input))
    handle1.close()





    share|improve this answer
























    • file_write() is working well. Problem is with excell workbook

      – ivbtar
      Nov 29 '18 at 14:25
















    0














    The problem is most likely with your file_write function, where you need to set the encoding of the file to be able to handle utf-8. In python3, you can do that using:




    def file_write(input):
    handle1=open('/tmp/filelog.txt','a', encoding='utf-8')
    handle1.write(str(input))
    handle1.close()





    share|improve this answer
























    • file_write() is working well. Problem is with excell workbook

      – ivbtar
      Nov 29 '18 at 14:25














    0












    0








    0







    The problem is most likely with your file_write function, where you need to set the encoding of the file to be able to handle utf-8. In python3, you can do that using:




    def file_write(input):
    handle1=open('/tmp/filelog.txt','a', encoding='utf-8')
    handle1.write(str(input))
    handle1.close()





    share|improve this answer













    The problem is most likely with your file_write function, where you need to set the encoding of the file to be able to handle utf-8. In python3, you can do that using:




    def file_write(input):
    handle1=open('/tmp/filelog.txt','a', encoding='utf-8')
    handle1.write(str(input))
    handle1.close()






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 23 '18 at 19:38









    2ps2ps

    8,16221131




    8,16221131













    • file_write() is working well. Problem is with excell workbook

      – ivbtar
      Nov 29 '18 at 14:25



















    • file_write() is working well. Problem is with excell workbook

      – ivbtar
      Nov 29 '18 at 14:25

















    file_write() is working well. Problem is with excell workbook

    – ivbtar
    Nov 29 '18 at 14:25





    file_write() is working well. Problem is with excell workbook

    – ivbtar
    Nov 29 '18 at 14:25













    0














    At last,



    Solution is :




    worksheet.write("A4", teachertitle.decode('utf-8'), titlescell)




    Decoding solved it. As i understand excell workbook needs the string to be decoded before writing to excell sheet.






    share|improve this answer




























      0














      At last,



      Solution is :




      worksheet.write("A4", teachertitle.decode('utf-8'), titlescell)




      Decoding solved it. As i understand excell workbook needs the string to be decoded before writing to excell sheet.






      share|improve this answer


























        0












        0








        0







        At last,



        Solution is :




        worksheet.write("A4", teachertitle.decode('utf-8'), titlescell)




        Decoding solved it. As i understand excell workbook needs the string to be decoded before writing to excell sheet.






        share|improve this answer













        At last,



        Solution is :




        worksheet.write("A4", teachertitle.decode('utf-8'), titlescell)




        Decoding solved it. As i understand excell workbook needs the string to be decoded before writing to excell sheet.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 29 '18 at 14:27









        ivbtarivbtar

        140110




        140110






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53448618%2fencode-decode-problem-while-writing-to-excell-worksheet-in-django-view%23new-answer', 'question_page');
            }
            );

            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







            Popular posts from this blog

            Paul Cézanne

            UIScrollView CustomStickyHeader Resize height generates problems when scroll is too fast

            Angular material date-picker (MatDatepicker) auto completes the date on focus out