Hi,
I’ve to translate chinese texts from a dwg file.
I was thinking of a PythonScript in Rhino to export each text of my model in a CSV file in order to open it in MS Excel, then translate every text content, and finally import back the translated text in the original model by replacing text objects’texts.
I can build the main workflow but I am facing difficulties with the encoding for chinese characters.
Message: 'ascii' codec can't encode character '\u5BA4' in position 0: ordinal not in range(128)
Is there someone who could help with this?
Thanks
What is the code that gives this error? Preferrably both the string construction and the string usage (probably the writing to the text file?)
Here is my code :
# -*- coding: utf-8 -*-
import rhinoscriptsyntax as rs
# extract texts in the model
textList = []
for object in rs.AllObjects():
if rs.IsText(object):
textList.append(rs.TextObjectText(object))
# create a filename variable
filename = "C:\\Users\\XXXX\\Desktop\\Exportation.csv"
file = open(filename, 'w')
#create and write a header for the CSV file
headerList=["Index","中国","English","Français"]
header = ";".join(headerList)+"\n"
file.write(header)
#create and write a line in CSV file for every text in the model
lineList=[]
i=0
for text in textList:
line = [i,text]
i+=1
for line in lineList:
fileLine = ";".join(line)+"\n"
file.write(fileLine)
file.close()
And a Rhino 6 file for testing :
Chinese text exportation.3dm (39.6 KB)
Thank you for watching.
Python is extremely finnicky with non-ascii strings. You’ll have to explicitly encode all strings to utf-8 to have it work properly. Including your header:
# -*- coding: utf-8 -*-
import rhinoscriptsyntax as rs
# extract texts in the model
textList = []
for o in rs.AllObjects():
if rs.IsText(o):
# explicitly encode to utf-8
s = rs.TextObjectText(o).encode('utf-8')
textList.append(s)
# create a filename variable
filename = "C:\\Users\\XXX\\Desktop\\Exportation.csv"
file = open(filename, 'w')
#create and write a header for the CSV file
headerList=[u'Index',u'中国',u'English',u'Français']
# explicitly encode to utf-8
headerList = [i.encode('utf-8') for i in headerList]
header = u"{}\n".format(u';'.join(headerList))
file.write(header)
#create and write a line in CSV file for every text in the model
lineList=[]
i=0
for text in textList:
line = [str(i),text]
i+=1
lineList.append(line)
for line in lineList:
fileLine = u';'.join(line)+u'\n'
file.write(fileLine)
file.close()
utf8_export.py (917 Bytes)
This creates from your 3dm
Exportation.csv.txt (158 Bytes) (Don’t forget to remove the .txt bit)
Thank you very much, it does the job perfectly !