尝试使用特殊字符作为输出时,Python、OpenSCAD 和 SolidPython 出现问题

问题描述 投票:0回答:1

所以我正在编写一个程序来自动制作带有名称的项目,并且在尝试使用 特殊字符 ž đ š ć č 渲染模型时遇到问题,我猜它无法翻译它们,它就结束了起来崩溃了。我在这里发布代码并在其中解释一下。

This is part of the program, where the issue is 

import pandas as pd
from solid import *
import subprocess
import time

if name == ‘main’:
# Read the Excel file

df = pd.read_excel(r"C:\Users\Miki\Desktop\data.xlsx" ,sheet_name='Sheet1')
#print(df.iterrows)
num_rows = len(df)
#print(df)
#print(df.shape)
# Iterate through each row of the Excel sheet
for index, row in df.iterrows():
# Read values from the first row and first three columns
    annotation1 = row.iloc[2] #1st phone number
    annotation2 = row.iloc[0] #Name
    annotation3 = row.iloc[3] #2nd phone number
    annotation4 = row.iloc[1] #Surname
    full_name = f"{annotation2} {annotation4}"
    **#full_name = str(annotation2)+" "+str(annotation4)**
    #print(full_name)
  
    #print(annotation1,annotation2,annotation3,annotation4)
    # Generate the assembly
    a = assembly(annotation1,str(full_name), annotation3)
  
    # Output the SCAD file with dynamic name based on annotation1
    scad_file = f"export_{annotation2}.scad"
    scad_file_location = f"C:\\Users\\Miki\\Desktop\\SCADfiles"
    scad_render_to_file(a, scad_file, include_orig_code=False)

   

    ######### Problem ko hočem renderat file ne gre, ker ima "full_name" presledek in ga unicoda ne sprejme. help
    #Problem when i want to render the file, it doesnt let me since the "full name" has restricted character "\t" and doesnt work then i guess xD

    # Run OpenSCAD to export the STL file
   ** stl_file = f"{annotation2}.stl"
    command = f"openscad -o C:\\Users\\Miki\\Desktop\\STLfiles\\{stl_file} C:\\Users\\Miki\\Desktop\\SCADfiles\\{scad_file}"
    subprocess.run(command, shell=True)
    time.sleep(50)
    print(full_name, "je opravljen.")***
This here is the error given back, as you can see it crashes at scad_render_to_file where var a has a var within itself “full_name” that contains a special char.


这是返回的错误,您可以看到它在 scad_render_to_file 处崩溃,其中 var a 本身有一个 var “full_name”,其中包含一个特殊字符。

Traceback (most recent call last):
File “c:\Users\Miki\Desktop\Model_cut_v3.py”, line 87, in
scad_render_to_file(a, scad_file, include_orig_code=False)
File “C:\Users\Miki\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\solid\solidpython.py”, line 518, in scad_render_to_file
return _write_code_to_file(rendered_string, filepath, out_dir, include_orig_code)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\Miki\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\solid\solidpython.py”, line 562, in _write_code_to_file
out_path.write_text(rendered_string)
File “C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.12_3.12.752.0_x64__qbz5n2kfra8p0\Lib\pathlib.py”, line 1048, in write_text
return f.write(data)
^^^^^^^^^^^^^
File “C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.12_3.12.752.0_x64__qbz5n2kfra8p0\Lib\encodings\cp1252.py”, line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
UnicodeEncodeError: ‘charmap’ codec can’t encode character ‘\u010d’ in position 555: character maps to

所以我想知道如何解决这个问题。有没有办法在 OpenSCAD 中对其进行解码?虽然我不知道这是否可能。

如果有人有任何想法如何解决这个问题,我真的很感激。

我尝试将其编码为ascii,结果每个字母都有很好的ascii值,这不是我想要的。我也尝试用 utf-8 对其进行编码,但结果又不太令人满意。 如果有人可以帮忙,谢谢

python character-encoding char openscad
1个回答
0
投票
    scad_file_location = f"C:\\Users\\Miki\\Desktop\\SCADfiles\\export_{annotation2}.scad"
    with open(scad_file_location, "w", encoding="utf8") as f:
       f.write(scad_render(model))
      
   
    
© www.soinside.com 2019 - 2024. All rights reserved.