Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving data from python to excel file as CSV UTF-8 file format

I have been trying to save the data as a excel file as a type of
CSV UTF-8 (Comma delimited) (*.csv) which is different then the normal
CSV (Comma delimited) (*.csv) file. It display the unicode text when opened in excel. I can save as that file easily from excel but from python i am only able to save it as normal csv. Which will not cause loss of data but when opened it shows this kind of text "à¤à¤‰à¤Ÿà¤¾" instead of "एउटा" this text.

If I copied the text opening it with notepad to the excel file and then manually save the file as CSV UTF-8 then it preserves the correct display. But doing so is time consuming since all values appear in same line in notepad and i have to separate it in excel file. So i just want to know how can i save data as CSV UTF-8 format of excel using python.

I have tried the follwing code but it results in normal csv file.

import codecs
import unicodecsv as csv

input_text = codecs.open('input.txt', encoding='utf-8')
all_text = input_text.read()
text_list = all_text.split()

output_list = [['Words','Tags']]
for input_word in text_list:
    word_tag_list = [input_word,'O']
    output_list.append(word_tag_list)

with codecs.open("output.csv", "wb") as f:
    writer = csv.writer(f)
    writer.writerows(output_list)
like image 856
Sujil Devkota Avatar asked Sep 04 '26 13:09

Sujil Devkota


1 Answers

You need to indicate to Excel that this is a UTF-8 file. Unfortunately the only way to do this is by prepending a special byte sequence to the front of the file. Python will do this automatically if you use a special encoding.

with codecs.open("output.csv", "w", encoding="utf_8_sig") as f:
like image 164
Mark Ransom Avatar answered Sep 06 '26 02:09

Mark Ransom



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!