๐Ÿ’ซ CSV ํŒŒ์ผ ์ธ์ฝ”๋”ฉ ์ •๋ณด ํ™•์ธํ•˜๊ธฐ

  • chardet ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์„ค์น˜
pip install chardet
  • Python ์ฝ”๋“œ
import chardet

# ํŒŒ์ผ ๊ฒฝ๋กœ
file_path = '1.csv'

# ํŒŒ์ผ ์—ด๊ธฐ (๋ฐ”์ด๋„ˆ๋ฆฌ ๋ชจ๋“œ)
with open(file_path, 'rb') as f:
    # ํŒŒ์ผ ๋‚ด์šฉ ์ฝ๊ธฐ
    content = f.read()

# ํŒŒ์ผ ๋‚ด์šฉ์˜ ์ธ์ฝ”๋”ฉ ์ถ”์ •
result = chardet.detect(content)

# ์ถ”์ •๋œ ์ธ์ฝ”๋”ฉ ์ถœ๋ ฅ
print("ํŒŒ์ผ์˜ ์ธ์ฝ”๋”ฉ:", result['encoding'])

ย 

๐Ÿ’ซ CSV ํŒŒ์ผ ์ธ์ฝ”๋”ฉ ์ •๋ณด ๋ณ€๊ฒฝํ•˜๊ธฐ

  • Python ์ฝ”๋“œ
import pandas as pd

# ์›๋ณธ CSV ํŒŒ์ผ๋ช…
input_file = '1.csv'

# ๋ณ€๊ฒฝ๋œ CSV ํŒŒ์ผ๋ช…
output_file = '2.csv'

# CSV ํŒŒ์ผ ์ฝ๊ธฐ (UTF-8 ์ธ์ฝ”๋”ฉ์œผ๋กœ ๊ฐ€์ •)
df = pd.read_csv(input_file)

# ๋ฐ์ดํ„ฐํ”„๋ ˆ์ž„์„ EUC-KR ์ธ์ฝ”๋”ฉ์œผ๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ์ €์žฅ
df.to_csv(output_file, encoding='euc-kr', index=False)

print("ํŒŒ์ผ์ด ์„ฑ๊ณต์ ์œผ๋กœ ์ €์žฅ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.")

By Dozzing

๋‹ต๊ธ€ ๋‚จ๊ธฐ๊ธฐ

์ด๋ฉ”์ผ ์ฃผ์†Œ๋Š” ๊ณต๊ฐœ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ํ•„์ˆ˜ ํ•„๋“œ๋Š” *๋กœ ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค