-
-
Notifications
You must be signed in to change notification settings - Fork 23
Description
Hi - I was getting some unicode decode error trying to run this, and asked gpt for help. it fixed the code and gave me this to share with you here.
Here's the description for the GitHub issue:
Issue: UnicodeEncodeError When Writing Files
Description
When attempting to write files using the write_file function, a UnicodeEncodeError occurs if the text contains characters not supported by the default encoding (cp1252). This error is specifically encountered with the character \u2028.
Error Message
Traceback (most recent call last):
File "C:\Users\naman\SandBox\1. Projects\3. Software\external-tools\tkforge\tkforge.py", line 63, in <module>
main()
File "C:\Users\naman\SandBox\1. Projects\3. Software\external-tools\tkforge\tkforge.py", line 55, in main
code = tk_code(extract_figma_id(file_id), token, output)
File "C:\Users\naman\SandBox\1. Projects\3. Software\external-tools\tkforge\tk.py", line 271, in tk_code
write_file(template, out)
File "C:\Users\naman\SandBox\1. Projects\3. Software\external-tools\tkforge\core.py", line 19, in write_file
file.write(text)
File "C:\Users\naman\AppData\Local\Programs\Python\Python310\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 '\u2028' in position 884: character maps to <undefined>
Steps to Reproduce
- Use the
write_filefunction to write text that includes characters outside thecp1252encoding range. - Run the script and observe the error.
Proposed Solution
Modify the write_file function to open the file with UTF-8 encoding, which supports a wider range of characters. This can be achieved by specifying the encoding parameter when opening the file.
Code Changes
In the write_file function, update the open call as follows:
def write_file(text, out=None, frame=None):
if out is None:
folder_path = 'TkForge'
else:
folder_path = os.path.join(out, 'TkForge')
if not os.path.exists(folder_path):
os.makedirs(folder_path)
if frame is not None:
file_name = f'frame_{frame}.py'
else:
file_name = 'main.py'
with open(os.path.join(folder_path, file_name), 'w', encoding='utf-8') as file: # Added encoding='utf-8'
file.write(text)This change ensures that the file is opened with UTF-8 encoding, preventing UnicodeEncodeError for characters not supported by cp1252.
References
You can post this description as an issue on the relevant GitHub repository to help the maintainer address the problem.