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 a question.

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

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4094310
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T19:43:44+00:00 2026-05-20T19:43:44+00:00

I tried to just use the ‘w’ tag while opening the file, but it

  • 0

I tried to just use the ‘w’ tag while opening the file, but it double spaced the lines which caused the read to not work. So I found that changing to ‘wb’ will be the correct formatting. Now that I am using the ‘wb’ flag I can’t get the csv.writer.writerow() to work. I have encoded all my strings and am lost as to why I keep getting this error. All the questions I see say that b’string here’ or myString.encode(‘ascii’) solves the error I get, but it is not solving it for me. Here is what I have:

    dataWriter = csv.writer(open(fileName, 'wb'))
    for i in range(self.ui.table.rowCount()):
        rowData = [self.ui.table.item(i,0).text().encode('utf-8')\
        ,self.ui.table.item(i,1).text().encode('utf-8')\
        ,self.ui.table.item(i,2).text().encode('utf-8')\
        ,self.ui.table.item(i,3).text().encode('utf-8')\
        ,self.ui.table.item(i,4).text().encode('utf-8')]
        dataWriter.writerow(rowData)

Which I figured would work but it still gives me the following error:
“TypeError: must be bytes or buffer, not str”
on the line “dataWriter.writerow(rowData).

Any help would be apreciated.
Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T19:43:44+00:00Added an answer on May 20, 2026 at 7:43 pm

    You appear to be running Python 3.x. Advice about using binary mode for csv files applies to Python 2.x. The codecs module is not required for 3.x — just use encoding=whatever when you open the file. What is needed for 3.x is that the file be opened with newline=''. This applies to both reading and writing, although it is not documented for writing (bug report has been submitted). After sorting out your doble-spacing problem, this will work:

    import csv
    data = [
        ['\xfforick', 123.456],
        ['polonius', 987.564],
        ]
    with open('demo.csv', 'w', newline='', encoding='utf8') as f:
        writer = csv.writer(f)
        for row in data:
            writer.writerow(row)
    

    Contents of output file:

    >>> open('demo.csv', 'rb').read()
    b'\xc3\xbforick,123.456\r\npolonius,987.564\r\n'
    >>>
    

    Suggestion: give some consideration to legibility of your code … instead of

    for i in range(self.ui.table.rowCount()):
        rowData = [self.ui.table.item(i,0).text().encode('utf-8')\
        ,self.ui.table.item(i,1).text().encode('utf-8')\
        ,self.ui.table.item(i,2).text().encode('utf-8')\
        ,self.ui.table.item(i,3).text().encode('utf-8')\
        ,self.ui.table.item(i,4).text().encode('utf-8')]
        dataWriter.writerow(rowData)
    

    try

    table = self.ui.table
    for i in range(table.rowCount()):
        row = [table.item(i, j).text() for j in range(5)]
        writer.writerow(row)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just tried to use JCurses from within Groovy, but I always get the
I just tried to use achartengine demo, but every time I click on something
I just tried to use the KeychainItemWrapper provided by Apple, but I'm getting a
I just tried to use an empty Security File. Although, System.exit(0); does work. Is
If I just use File to install fonts, installation goes fine, but the fonts
I tried to use dat.GUI in the following three.js example . I just did
I just tried the following code, but the result seems a little strange. It
I have just tried to save a simple *.rtf file with some websites and
I tried just making the image switch to black and then use sleep(1) and
I just tried to use Gears in my GWT application and got following error:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

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.