Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.(5)

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/ Questions/Typeerror: write() argument must be str, not bytes - simple solution when encountering the error.
Next
Answered
Livia Brown
  • 14
Livia Brown
Asked: May 17, 20222022-05-17T14:21:16+00:00 2022-05-17T14:21:16+00:00In: python

Typeerror: write() argument must be str, not bytes – simple solution when encountering the error.

  • 14

. Advertisement .

..3..

. Advertisement .

..4..

I get the error: Typeerror: write() argument must be str, not bytes when I try to run the program below:

# -*- coding: utf-8 -*-
 import time
 from datetime import date
 from lxml import etree
 from collections import OrderedDict
 
 # Create the root element
 page = etree.Element('results')
 
 # Make a new document tree
 doc = etree.ElementTree(page)
 
 # Add the subelements
 pageElement = etree.SubElement(page, 'Country',Tim = 'Now', 
  name='Germany', AnotherParameter = 'Bye',
  Code='DE',
  Storage='Basic')
 pageElement = etree.SubElement(page, 'City', 
  name='Germany',
  Code='PZ',
  Storage='Basic',AnotherParameter = 'Hello')
 # For multiple multiple attributes, use as shown above
 
 # Save to XML file
 outFile = open('output.xml', 'w')
 doc.write(outFile)

The error appears the system notifies as follows:

builtins.TypeError: must be str, not bytes
 File "C:\PythonExamples\XmlReportGeneratorExample.py", line 29, in <module>
  doc.write(outFile)
 File "c:\Python32\Lib\site-packages\lxml\etree.pyd", line 1853, in lxml.etree._ElementTree.write (src/lxml/lxml.etree.c:44355)
 File "c:\Python32\Lib\site-packages\lxml\etree.pyd", line 478, in lxml.etree._tofilelike (src/lxml/lxml.etree.c:90649)
 File "c:\Python32\Lib\site-packages\lxml\etree.pyd", line 282, in lxml.etree._ExceptionContext._raise_if_stored (src/lxml/lxml.etree.c:7972)
 File "c:\Python32\Lib\site-packages\lxml\etree.pyd", line 378, in lxml.etree._FilelikeWriter.write (src/lxml/lxml.etree.c:89527)

I tried to solve it with another sample. I got the reference in the community forum, but it still returned an invalid result. If someone knows the solution, please give me the support. Thanks!

write() argument must be str
  • 2 2 Answers
  • 110 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

2 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    dttutoria Expert
    2022-06-10T02:28:48+00:00Added an answer on June 10, 2022 at 2:28 am

    The cause:

    Typeerror: write() argument must be str, not bytes appears as your code is not right in the below line:

    outFile = open('output.xml', 'w')

    Solution:

     Python request must be str, but not bytes. So, the outfile needs to be converted into binary mode.

    outFile = open('output.xml', 'wb')
    • 9
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. Rose Roger
    2022-05-25T20:07:38+00:00Added an answer on May 25, 2022 at 8:07 pm

    Convert binary files to base64 and vice versa In python 3.5.2

    import base64
    
    read_file = open('/tmp/newgalax.png', 'rb')
    data = read_file.read()
    
    b64 = base64.b64encode(data)
    
    print (b64)
    
    # Save file
    decode_b64 = base64.b64decode(b64)
    out_file = open('/tmp/out_newgalax.png', 'wb')
    out_file.write(decode_b64)
    
    # Test in python 3.5.2
    • 21
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.