GithubHelp home page GithubHelp logo

Calling writer.writerow on a list of strings in python 3.5 results in TypeError: string argument expected, got 'bytes' about python-unicodecsv HOT 4 CLOSED

jdunck avatar jdunck commented on August 29, 2024
Calling writer.writerow on a list of strings in python 3.5 results in TypeError: string argument expected, got 'bytes'

from python-unicodecsv.

Comments (4)

ryanhiebert avatar ryanhiebert commented on August 29, 2024 1

Ah, you know what, I'll bet you're opening the file that you're writing to in text mode. unicodecsv must write to a binary mode file, because unicodecsv takes care of encoding all the data to bytes.

from python-unicodecsv.

ben-en avatar ben-en commented on August 29, 2024 1

That was the solution! I was using a StringIO object. Thanks again!

from python-unicodecsv.

ryanhiebert avatar ryanhiebert commented on August 29, 2024

I'm afraid that I'm not able to reproduce this. Here's my terminal, with my attempt, perhaps you can see where I did things differently:

$ pip install unicodecsv
Collecting unicodecsv
Installing collected packages: unicodecsv
Successfully installed unicodecsv-0.14.1
$ python
Python 3.5.0 (default, Sep 14 2015, 09:14:42)
[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import io
>>> f = io.BytesIO()
>>> import unicodecsv as csv
>>> writer = csv.writer(f)
>>> row = ['http://localhost:10081/content/HiMFYeCsAfJxP9Jc/allianceearth.org', 'South African team may have solved solar puzzle even Google couldn’t crack', 'ARL', 'ephemeral', 'rss_feeder', '2015-11-30 21:53:07 ', '', '']
>>> writer.writerow(row)
192
>>> f.seek(0)
0
>>> f.readlines()
[b'http://localhost:10081/content/HiMFYeCsAfJxP9Jc/allianceearth.org,South African team may have solved solar puzzle even Google couldn\xe2\x80\x99t crack,ARL,ephemeral,rss_feeder,2015-11-30 21:53:07 ,,\r\n']
>>>

from python-unicodecsv.

ben-en avatar ben-en commented on August 29, 2024

Hm, interesting. It does seem like that would be the most likely culprit. I'll have a look at that, thank you.

from python-unicodecsv.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.