0

I have a _csv.reader type object that has been created by a function gathering data from an API. How would I open and parse this type of object, for processing in Pandas etc?

The object was initially created in this way:

obj = csv.reader(io.BytesIO(Results.encode('ascii', 'replace')), delimiter='\t')
tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Yes but it did not work. Trying to iterate through the object gives: Error: iterator should return strings, not bytes (did you open the file in text mode?) – littlefield Dec 07 '21 at 18:52
  • 1
    Ok, the error messge seems pretty self-explanatory... why are you passing a `io.BytesIO` object instead of a `io.StringIO`? the reader object requires strings, not bytes – juanpa.arrivillaga Dec 07 '21 at 18:53
  • What is a `_csv.reader`? From the prefix it looks like a private attribute. – martineau Dec 07 '21 at 19:51
  • Thanks @juanpa.arrivillaga changing to stringIO worked (see edit). Doing the same with the BytesIO object would be nice to know but not necessary – littlefield Dec 08 '21 at 09:35
  • @littlefield you can't, `csv` reader objects require strings. – juanpa.arrivillaga Dec 08 '21 at 18:59

1 Answers1

0

Changing to StringIO worked. I believe BytesIO was used due to the encoding, possibly to ignore greek characters?

with io.StringIO(Results) as file: csv_reader = csv.reader(file, delimiter='\t') for row in csv_reader: print(row)

  • "I believe BytesIO was used due to the encoding, possibly to ignore greek characters?" No, almost certainly, you were looking at an old example (Python 2) – juanpa.arrivillaga Dec 08 '21 at 19:00