i want to write a date in a csv file (excel) using pythom 3.3 here is my code
date=["16.02.2018", "blah"]
CSVreport = open('indata.csv', 'wb')
wr = csv.writer(CSVreport)
with open('indata.csv', 'w+') as ou_f:
ou_f.write([str(date,'UTF-8')])
with open('indata.csv', 'r') as in_f:
ou_f_content = in_f.read().decode('UFT-8')
#wr.writerow (bytes(date,'UTF-8'))
CSVreport.close()
an it gives me the error in the title I don't know what's wrong so if you can help I will be thankfull
You have at least four problems, three of which are on the line ou_f.write([str(date,'UTF-8')])
.
date
is a list and you cannot convert a list to a string with str()
.ou_f.write()
, but it requires a string.-
with open('indata.csv', 'w') as CSVreport:
wr = csv.writer(CSVreport)
wr.writerow(date)
u mispelled utf on your write line
also check out the struct module for packing and unpacking binary data, which is what utf8 encoding does. you can check to see what you're actually passing to the write() function, also i think your script itself has to be utf8 encoded
'w' opens for ascii pack/unpack, 'wb' writes the data as you pass it. 'w' is pretty much a shortcut for python to do the packing internally for ascii. if you're writing utf-8 data you should research if 'w' can handle this correctly. and its questionable if you even need to use utf-8. ascii covers 256 characters which includes the american date/time notation. also, why not close the file?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.