I using PythonShell get category of selected element, in Revit English show ‘walls’ but Revit Japanese version show [u’\u58c1’]
How to fix it.
from Autodesk.Revit.DB import Element
doc = revit.ActiveUIDocument.Document
uidoc = revit.ActiveUIDocument
selection = [doc.GetElement(x) for x in uidoc.Selection.GetElementIds()]
#get category
lst =
for i in selection:
lst.append(i.Category.Name)
Looks like a unicode character, I come across these in pyrevit a fair bit in the console. In addition to specifying utf8 formatting I have found that using encode and decode is often required to make sure special characters are dealt with as unicode objects vs strings. Not sure if the revit python shell can show unicode characters though.
from Autodesk.Revit.DB import Element
doc = __revit__.ActiveUIDocument.Document
uidoc = __revit__.ActiveUIDocument
selection = [doc.GetElement(x) for x in uidoc.Selection.GetElementIds()]
category_name = []
for i in selection:
print i.Category.Name
print('_____error: after creat list____')
for i in selection:
category_name.append(i.Category.Name)
print(category_name)
I managed to get Japanese to print using this approach in pyRevit, which also probably works in the Pythonshell (I don’t have it installed).
# -*- coding: UTF-8 -*-
import csv
filePath = r'** your file path here**'
data = []
with open(filePath) as csvfile:
reader = csv.reader(csvfile, delimiter=' ')
for row in reader:
data.append(row[0])
for d in data:
printable = d.decode('utf-8', 'ignore')
print(printable)
I read a csv file with the following contents:
平仮名
平仮名
平仮名
See below it working all together on my (English) computer. If this doesn’t work then it’s likely an issue related to the computer or Revit language that I can’t test any further myself.
Thank you!
in your script if I add line ‘print(data)’ to line 12 (meaning print ‘List’) => error
I understand your script but seem like problem in here is:
when creat ‘Empty List’ and then ‘append’ iterator to it will show error
=> I want creat List from iterator
Then you will need to decode the text as you append it to data in that case. A list containing encoded objects will have the same issue if printed as trying to print a unicode object.
Use the decode method like I’ve shown, and hopefully it works. Decode the text too, not the list object (data).
import clr
import sys
import System
from cStringIO import StringIO
# redirect the sys.stdout
sys.stdout = StringIO()
inputstr = "牆"
# print a single variable
print(inputstr )
a_list = [inputstr , inputstr , inputstr ]
# print a list
print(a_list)
# set to result to OUT variable
sys.stdout.seek(0)
OUT = sys.stdout.read().decode("unicode-escape")
# reset the sys.stdout
sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__
# print the result in console
print(OUT)