1

I am currently looking to build a script that takes .txt files (tab delimited) and converts them to .csv. I am getting and error that says (a bytes-like object is required, not 'str') when running the following code. What is the best way to complete this operation?

  import csv
  import itertools

  txt_file_P_T = r"mytxt_P_T.txt"
  txt_file_P_C = r"mytxt_P_C.txt"
  txt_file_S_T = r"mytxt_S_T.txt"
  txt_file_S_C = r"mytxt_S_C.txt"

  csv_file_P_T = r"mycsv_P_T.csv"
  csv_file_P_C = r"mycsv_P_C.csv"
  csv_file_S_T = r"mycsv_S_T.csv"
  csv_file_S_C = r"mycsv_S_C.csv"

  text_list = [txt_file_P_T, txt_file_P_C, txt_file_S_T, txt_file_S_C]
  csv_list = [csv_file_P_T, csv_file_P_C, csv_file_S_T, csv_file_S_C]

  for i, j   in zip(text_list, csv_list):
      in_txt = csv.reader(open(i, "rt"),)
      out_csv = csv.writer(open(j, 'wb'))
      out_csv.writerows(in_txt)
mitch
  • 379
  • 1
  • 3
  • 14
  • 1
    change the `wb` to `w` in `csv.writer(open(j, 'wb'))` – pault Jul 31 '18 at 14:55
  • 1
    Possible duplicate of [python 3.5: TypeError: a bytes-like object is required, not 'str' when writing to a file](https://stackoverflow.com/questions/33054527/python-3-5-typeerror-a-bytes-like-object-is-required-not-str-when-writing-t) – pault Jul 31 '18 at 14:56
  • @pault: not exactly a duplicate: mode text is required for a csv writer, but `endline=''` must be provided too. – Serge Ballesta Jul 31 '18 at 15:44

1 Answers1

2

The csv module was heavily changed between Python2 and Python3. In Python2, the underlying file object for csv writer should be opened in binary mode. In Python3 it should be opened it text mode (which allows to specify an explicit encoding) and with the newline = '' parameter to avoid any end of line conversion.

So you should have:

...
for i, j   in zip(text_list, csv_list):
      in_txt = csv.reader(open(i))
      out_csv = csv.writer(open(j, 'w', newline = ''))
      out_csv.writerows(in_txt)
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252