반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
Tags
- list
- Python
- google apps script
- Tkinter
- Java
- hive
- Redshift
- PySpark
- 파이썬
- Apache
- string
- SQL
- GIT
- PANDAS
- PostgreSQL
- Github
- matplotlib
- gas
- Google Spreadsheet
- Kotlin
- dataframe
- array
- Mac
- Google Excel
- Excel
- numpy
- c#
- math
- django
Archives
- Today
- Total
달나라 노트
Python openpyxl : ILLEGAL_CHARACTERS (python to_excel illegal character error) 본문
Python/Python ETC
Python openpyxl : ILLEGAL_CHARACTERS (python to_excel illegal character error)
CosmosProject 2025. 4. 24. 22:05728x90
반응형
Python으로 데이터를 다루거나 특히 pandas의 to_excel로 엑셀 파일을 생성할 때 illegal character error가 발생할 때가 있습니다.
이는 DataFrame의 어딘가에 illegal character가 포함되어있기 때문이라 이를 제거해주어야 합니다.
from openpyxl.cell.cell import ILLEGAL_CHARACTERS_RE
...
df.loc[:, 'col1'] = df.apply(
lambda row: ILLEGAL_CHARACTERS_RE.sub(r'', row['col1']),
axis=1
)
illegal character는 위처럼 openpyxl library의 ILLEGAL_CHARACTERS_RE method를 이용하여 특정 column에 존재하는 illegal character를 제거할 수 있습니다.
(단순
from openpyxl.cell.cell import ILLEGAL_CHARACTERS_RE
val_text = 'asidfhasuehfojw4098wjfois'
val_text = ILLEGAL_CHARACTERS_RE.sub(r'', val_text)
print(val_text)
ILLEGAL_CHARACTERS_RE method는 위처럼 일반 텍스트에도 적용할 수 있습니다.
FYI
만약 pandas에서 to_excel을 사용하는 경우에 한해서라면 ILLEGAL_CHARACTERS_RE를 사용하지 않고 xlsxwriter library를 설치해도 해결되는 경우가 있습니다.
728x90
반응형
'Python > Python ETC' 카테고리의 다른 글
Python pynput : keyboard (python으로 키보드 컨트롤하기, Python으로 keyboard control) (0) | 2024.11.05 |
---|---|
Python pynput : mouse (python으로 마우스 컨트롤하기, Python으로 mouse control) (1) | 2024.11.05 |
Python PIL : 화면 캡쳐하기 (screen capture with python) (0) | 2024.11.05 |
PIL을 이용하여 이미지 위에 글씨 쓰기 (2) | 2024.03.07 |
ImportError: module 'cv2.dnn' has no attribute 'DictValue' error (0) | 2024.03.07 |
Comments