15

I´m trying to transpose a matrix from a csv-file with the following code:

import csv
from itertools import izip
a = izip(*csv.reader(open("TDM.csv", "rb")))
csv.writer(open("output.csv", "wb")).writerows(a)

Unfortunately the following error occurs:

from itertools import izip
ImportError: cannot import name 'izip'

I already looked through the forums but couldn´t find the right answer for me.

MBT
  • 21,733
  • 19
  • 84
  • 102
Nils_Denter
  • 488
  • 1
  • 6
  • 18

1 Answers1

27

I guess you use Python 3.

Use zip() builtin instead.

In Python 3 there is no itertools.izip() as the zip() builtin behaves similarly.

abukaj
  • 2,582
  • 1
  • 22
  • 45
  • Using zip() still gives me an error: from itertools import zip ImportError: cannot import name 'zip' Code: import csv from itertools import zip a = zip(*csv.reader(open("TDM.csv", "rb"))) csv.writer(open("output.csv", "wb")).writerows(a) – Nils_Denter May 08 '18 at 18:10
  • 2
    @Nils_Denter do not import it. It is [a builtin](https://docs.python.org/3/library/functions.html) - it is always available (unless overwritten by an assignment). – abukaj May 08 '18 at 18:11
  • Ok now it works, but i get a new error: a = zip(*csv.reader(open("TDM.csv", "rb"))) Error: iterator should return strings, not bytes (did you open the file in text mode?) – Nils_Denter May 08 '18 at 18:14
  • @Nils_Denter Did you? I can see "b" as "binary". ;) – abukaj May 08 '18 at 18:16
  • Ah thank you, I´m really new to python. My mistake. – Nils_Denter May 08 '18 at 18:18
  • No need to import in python 3 just use zip() and it will work. – Alana Melo Mar 26 '22 at 22:36