Can not Get category in Revit Language Japanese by type data 'List'

Hi all!

I using PythonShell get category of selected element, in Revit English show ‘walls’ but Revit Japanese version show [u’\u58c1’]
How to fix it.

image

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)

print(lst)

@Aaron_Tang any ideas on how to help out @manhgt214 here? :pray:

1 Like

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.

3 Likes

I tried using Pyrevit to test code but name list show the same.

image

image

Try using encode/decode and specifying the unicode formatting in the script header:

Unicode HOWTO — Python 3.10.6 documentation.

Header for pyrevit script:
# -*- coding: UTF-8 -*-

1 Like

I tried using your code in the first line of script python but not solve…
This is Dialoge error when Pyrevit crash unicode but my case not show it.

I relized when creat list and append it => error

image


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.

1 Like

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

image

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).

1 Like

Hi,
try to join the list before print()

2 Likes

Hi Poupin
Can not get by type ‘List’

image

A workaround for a list (there is maybe a better solution)
tested in Autocad Python Shell

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)
1 Like

Hi, I tested but element of list have ‘u’, can you fix it.
Thank advance

Using replace()

1 Like

Hi
I dont understand why type(OUT) is ‘str’ not ‘list’ and then len(OUT) != 7, OUT[0] != ‘構造柱’

print() function show always the representation of object (it’s a basestring)
example, if you check the type of objects

1 Like

Thank you!!
I checked with Pyrevit and PythonShell.

1 Like