Python 3.x 如何让IfcOpenShell for python使用与其读取的文件相同的unicode进行编写?
我正在使用IfcOpenshell读取.ifc文件。进行一些更改,然后将其写入新的.ifc文件。但IfcOpenshell编写unicode的方式与读取unicode的方式不同 我正在创建一个脚本,在每个ifcelement中添加一个带有属性的pset。这些特性的值是从现有特性复制的。所以基本上我创建了一个pset,它将所选信息收集到一个地方。 在现有值包含unicode utf-8之前,这一直非常有效。 它被读取和解码以在打印时显示正确的值,但它写入unicode的方式与读取unicode的方式不同。 我试图改变PyCharm中使用的unicode,但没有成功。我在别处找到了simular帖子,但没有找到修复方法。 从我在别处读到的内容来看,它与IfcOpenshell使用的unicode编码器/解码器有关,但我不能确定Python 3.x 如何让IfcOpenShell for python使用与其读取的文件相同的unicode进行编写?,python-3.x,unicode,ifc,Python 3.x,Unicode,Ifc,我正在使用IfcOpenshell读取.ifc文件。进行一些更改,然后将其写入新的.ifc文件。但IfcOpenshell编写unicode的方式与读取unicode的方式不同 我正在创建一个脚本,在每个ifcelement中添加一个带有属性的pset。这些特性的值是从现有特性复制的。所以基本上我创建了一个pset,它将所选信息收集到一个地方。 在现有值包含unicode utf-8之前,这一直非常有效。 它被读取和解码以在打印时显示正确的值,但它写入unicode的方式与读取unicode的方
def mk_pset():
global param_name
global param_type
global max_row
global param_map
wb = load_workbook(b)
sheet = wb.active
max_row = sheet.max_row
max_column = sheet.max_column
param_name = []
param_type = []
param_map=[]
global pset_name
pset_name = sheet.cell(row=2, column=1).value
for pm in range(2, max_row+1):
param_name.append((sheet.cell(pm, 2)).value)
param_type.append((sheet.cell(pm, 3)).value)
param_map.append((sheet.cell(pm,4)).value)
print(param_type,' - ',len(param_type))
print(param_name,' - ',len(param_name))
create_pset()
def create_pset():
ifcfile = ifcopenshell.open(ifc_loc)
create_guid = lambda: ifcopenshell.guid.compress(uuid.uuid1().hex)
owner_history = ifcfile.by_type("IfcOwnerHistory")[0]
element = ifcfile.by_type("IfcElement")
sets = ifcfile.by_type("IfcPropertySet")
list = []
for sett in sets:
list.append(sett.Name)
myset = set(list)
global antall_parametere
global index
index = 0
antall_parametere = len(param_name)
if pset_name not in myset:
property_values = []
tot_elem = (len(element))
cur_elem = 1
for e in element:
start_time_e=time.time()
if not e.is_a() == 'IfcOpeningElement':
type_element.append(e.is_a())
for rel_e in e.IsDefinedBy:
if rel_e.is_a('IfcRelDefinesByProperties'):
if not rel_e[5][4] == None:
index = 0
while index < antall_parametere:
try:
ind1 = 0
antall_ind1 = len(rel_e[5][4])
while ind1 < antall_ind1:
if rel_e[5][4][ind1][0] == param_map[index]:
try:
if not rel_e[5][4][ind1][2]==None:
p_type = rel_e[5][4][ind1][2].is_a()
p_verdi =rel_e[5][4][ind1][2][0]
p_t=param_type[index]
property_values.append(ifcfile.createIfcPropertySingleValue(param_name[index], param_name[index],ifcfile.create_entity(p_type,p_verdi),None),)
ind1 += 1
else:
ind1 +=1
except TypeError:
pass
break
else:
ind1 += 1
except AttributeError and IndexError:
pass
index += 1
index = 0
property_set = ifcfile.createIfcPropertySet(create_guid(), owner_history, pset_name, pset_name,property_values)
ifcfile.createIfcRelDefinesByProperties(create_guid(), owner_history, None, None, [e], property_set)
ifc_loc_edit = str(ifc_loc.replace(".ifc", "_Edited.ifc"))
property_values = []
print(cur_elem, ' av ', tot_elem, ' elementer ferdig. ',int(tot_elem-cur_elem),'elementer gjenstår. Det tok ',format(time.time()-start_time_e),' sekunder')
cur_elem += 1
ifcfile.write(ifc_loc_edit)
else:
###print("Pset finnes")
sg.PopupError("Pset er allerede oprettet i modell.")
写入(D\X4\000000 F8\X0\r):
解码为“Dør”
硬空间也会发生这种情况:
('2\X2\00A0\X0\090')
正确打印为:('2 090')
写入:
('2\X4\000000A0\X0\090')
我的ifc使用软件无法读取书面形式。与其说是一种应答,不如说是一种解决方法 经过更多的研究,我发现大多数IFC阅读软件似乎不支持X4编码,所以我用regex做了一个变通方法。基本上查找所有内容并将\X4\0000替换为\X2。到目前为止,我遇到的所有规范角色都是这样。但如上所述,这只是一种可能不会对所有人都有效的变通方法
def X4trans_2(target_file,temp_fil):
from re import findall
from os import remove,rename
dec_file = target_file.replace('.ifc', '_dec.ifc')
tempname = target_file
dec_list = []
with open(temp_fil, 'r+') as r,open(dec_file, 'w', encoding='cp1252') as f:
for line in r:
findX4 = findall(r'\\X4\\0000+[\w]+\\X0\\', str(line))
if findX4:
for fx in findX4:
X4 = str(fx)
newX = str(fx).replace('\\X4\\0000', '\X2\\')
line = line.replace(str(X4), newX) # print ('Fant X4')
f.writelines(line)
remove(temp_fil)
try:
remove(target_file)
except FileNotFoundError:
pass
rename(dec_file,tempname)
它基本上以文本形式打开ifc,查找X4并用X2替换它,然后再次写入。与其说是一个Answare,不如说是一个解决方法 经过更多的研究,我发现大多数IFC阅读软件似乎不支持X4编码,所以我用regex做了一个变通方法。基本上查找所有内容并将\X4\0000替换为\X2。到目前为止,我遇到的所有规范角色都是这样。但如上所述,这只是一种可能不会对所有人都有效的变通方法
def X4trans_2(target_file,temp_fil):
from re import findall
from os import remove,rename
dec_file = target_file.replace('.ifc', '_dec.ifc')
tempname = target_file
dec_list = []
with open(temp_fil, 'r+') as r,open(dec_file, 'w', encoding='cp1252') as f:
for line in r:
findX4 = findall(r'\\X4\\0000+[\w]+\\X0\\', str(line))
if findX4:
for fx in findX4:
X4 = str(fx)
newX = str(fx).replace('\\X4\\0000', '\X2\\')
line = line.replace(str(X4), newX) # print ('Fant X4')
f.writelines(line)
remove(temp_fil)
try:
remove(target_file)
except FileNotFoundError:
pass
rename(dec_file,tempname)
它基本上以文本形式打开ifc,找到X4并用X2替换它,然后再次写入。对于大的类型感到抱歉。复制/粘贴结果。您的代码示例不完整。如何实例化变量
rel_e
,param_map
,param_type
,ifcfile
,property_值
…?编辑以包含更多代码。主要的软件包是“ifcopenshell”和“openpyxl”,很抱歉这些大的类型。复制/粘贴结果。您的代码示例不完整。如何实例化变量rel_e
,param_map
,param_type
,ifcfile
,property_值
…?编辑以包含更多代码。主要软件包是“ifcopenshell”和“openpyxl”
def X4trans_2(target_file,temp_fil):
from re import findall
from os import remove,rename
dec_file = target_file.replace('.ifc', '_dec.ifc')
tempname = target_file
dec_list = []
with open(temp_fil, 'r+') as r,open(dec_file, 'w', encoding='cp1252') as f:
for line in r:
findX4 = findall(r'\\X4\\0000+[\w]+\\X0\\', str(line))
if findX4:
for fx in findX4:
X4 = str(fx)
newX = str(fx).replace('\\X4\\0000', '\X2\\')
line = line.replace(str(X4), newX) # print ('Fant X4')
f.writelines(line)
remove(temp_fil)
try:
remove(target_file)
except FileNotFoundError:
pass
rename(dec_file,tempname)