2013-10-01 2 views
10

I 텍스트 파일 (쉼표로 구분 된 값) 아래 스크린 샷을 현재 기존 워크 시트를로드하고 가져 오기 위해 노력하고 있어요파이썬 던지기, 오류

엑셀 시트 " 'UTF8'코덱의 위치는 0 바이트 0xd0를 디코딩 할 수 없습니다" :

enter image description here

텍스트 파일 :

enter image description here

내가 코드를 사용하고 벨로 표시 W :

# importing necessary modules for performing the required operation 
    import glob 
    import csv 
    from openpyxl import load_workbook 
    import xlwt 

    #read the text file(s) using the CSV modules and read the dilimiters and quoutechar 
    for filename in glob.glob("E:\Scripting_Test\Phase1\*.txt"): 
     spamReader = csv.reader((open(filename, 'rb')), delimiter=',') 


     #read the excel file and using xlwt modules and set the active sheet 
     wb = load_workbook(filename=r"E:\Scripting_Test\SeqTem\Seq0001.xls") 
     ws = wb.worksheets(0) 


     #write the data that is in text file to excel file 
     for rowx, row in enumerate(spamReader): 
      for colx, value in enumerate(row): 
       ws.write(rowx, colx, value) 

     wb.save() 

나는 다음과 같은 오류 메시지가 점점 오전 :

UnicodeDecodeError: 'utf8' codec can't decode byte 0xd0 in position 0: invalid continuation byte

하나 더 질문 : 당신은 어떻게 엑셀 시트에 A3 열에서 시작하는 텍스트 데이터를 가져 Python을 알 수 있습니까?

답변

1

안녕 당신을 위치 : 여기

value = unicode(value, errors='ignore') 

유니 코드에 대한 자세한 독서에 대한 좋은 답변입니다 당신이 가지고있는 문서가 없다면 UTF-8 BOM

UTF-8 BOM codec와 함께 사용해 볼 수도 있습니다. 일반적으로 Windows + UTF + 8은 다소 골칫거리가 될 수 있습니다. 그것이 보여주는 캐릭터는 BOM이 아닐 수도 있습니다.

2

openpyxlOOXML 형식 (xlsx/xlsm) 만 취급합니다. Excel을 사용하여 xls 대신 xlsx 파일 형식으로 저장하십시오.

코드에서 xls 파일을 xlsx로 변환하려는 경우. 아래 목록에서 하나의 옵션을 시도하십시오 :

  1. 윈도우에서, 당신은 또한 xlxx하는 XLS 변환 excelcnv 도구를 사용할 수 있습니다.
  2. Linux의 경우 this article을 확인하십시오.
  3. 또는 파이썬에서 xlrd을 사용하여 xlsx로 변환 할 수 있습니다. this Q&A을 확인하십시오.