解码.txt - ' utf-8'编解码器无法解码字节0xf3

时间:2017-02-01 17:52:45

标签: python excel text encoding dns

我将数据,域,从excel文件转换为文本文件,然后检查域的可用性。当我从excel文件中获取数据后尝试使用该文本文件时,会弹出问题。

这是excel文件中的数据

arete.cl
cbsanbernardo.cl
ludala.cl
puntotactico.cl
sunriseskateboard.cl
ellegrand.cl
turismosantodomingo.cl
delotroladof.cl
produccionesmandala.cl

所以,基本上如果我手动输入文本文件中的域,脚本工作正常。但是,如果我将excel文件中的域转换为文本文件,然后运行该脚本,则会弹出以下错误:

UnicodeDecodeError:' utf-8'编解码器不能解码位置194中的字节0xf3:无效的连续字节

如果我尝试直接从excel文件中检查域,也会发生同样的情况。 我应该解码.txt或.xlsx吗?我该怎么办?

#!/usr/bin/python

import pythonwhois
import openpyxl
from openpyxl import load_workbook
import os

pathx = 'path'
filex = 'file.xlsx'

print('**Availability of domains**')
os.chdir(pathx)
workbook = openpyxl.load_workbook(filex, data_only = True)
sheet = workbook.get_sheet_by_name('Dic')


domainsz = io.open(pathx + '\\domains.txt', 'a')

for i in range(1, 10):
    domainx = sheet["A" + str(i * 2)].value
    if domainx is not None:
        domainsz.write(domainx + '\n')
        print(domainx)

domainsz.close()

with gzip.open('domains.txt' + ".gz", "wb") as outfile:
    outfile.write(bytes(plaintext, 'UTF-8'))

domains = []
available = []
unavailable = []


def getDomains():
    with io.open('domains.txt', 'r', encoding='latin-1') as f:
        for domainName in f.read().splitlines():
            domains.append(domainName)

def run():   
    for dom in domains:
        if dom is not None and dom != '':
            details = pythonwhois.get_whois(dom)
            if details['contacts']['registrant'] is not None:
                unavailable.append(dom)
            else:
            available.append(dom)

def printAvailability():
    print ("-----------------------------")
    print ("Unavailable Domains: ")
    print ("-----------------------------")
    for un in unavailable:
        print (un)
    print ("\n")
    print ("-----------------------------")
    print ("Available Domains: ")
    print ("-----------------------------")
    for av in available:
        print (av)


if __name__ == "__main__":
    getDomains()
    run()
    printAvailability()

0 个答案:

没有答案