i am trying to write string to csv file that i opened in 'wb' mode. i get the above error despite i provided to csv writer a byte object i used pdb to make sure that i am right
(Pdb) type(row.encode("utf-8"))
<class 'bytes'>
i know that i can open it only in 'w' mode but it should be compatible with python2.7 and in python 2.7 it insert redundant empty lines if i opens the files with 'w'. in addition that should be compatible i want to understand what i am doing wrong here.
rows_list=[]
rows_list.append('plimit')
rows_list.append('#i_pstate')
csvfile=open(output_file_path, 'wb')
try:
filewriter = csv.writer(csvfile, delimiter=',',
quotechar='|', quoting=csv.QUOTE_MINIMAL)
#import pdb;pdb.set_trace()
for row in rows_list:
filewriter.writerow([row.encode("utf-8")])
except Exception as ex:
print ("error occurred '%s'"% (ex))
return -1
finally:
csvfile.close()
full traceback:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "c:\pythonsv\icelake\debug\domains\ice\platform_config_generation.py", line 81, in write_max_pstate_config_csv
filewriter.writerow([row.encode("utf-8")])
TypeError: a bytes-like object is required, not 's