2014-11-25 2 views
0

어떻게 파이썬 스크립트를 통해 인코딩을 변경합니까?파이썬 UTF-8에서 UTF-16으로 csv 파일의 인코딩 변경

나는 다른 파일들을 반복하고있는 파일들을 가지고있다. 하지만 그 전에 SQL 서버가 UTF-8을 지원하지 않기 때문에 각 파일의 인코딩을 UTF-8에서 UTF-16으로 변경해야합니다.

시도했지만 작동하지 않습니다.

data = "UTF-8 data" 
udata = data.decode("utf-8") 
data = udata.encode("utf-16","ignore") 

건배! 당신이 UTF-16 인코딩 파일에 UTF-8 인코딩에서 파일을 변환 할 경우

답변

2

,이 스크립트는 작동합니다

#!/usr/bin/python2.7 

import codecs 
import shutil 

with codecs.open("input_file.utf8.txt", encoding="utf-8") as input_file: 
    with codecs.open(
      "output_file.utf16.txt", "w", encoding="utf-16") as output_file: 
     shutil.copyfileobj(input_file, output_file) 
+0

감사합니다! 그게 내 문제를 해결해 줬어. – SterlinkArcher