博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
sublime python3中读取和写入文件时如何解决编码问题
阅读量:4320 次
发布时间:2019-06-06

本文共 1465 字,大约阅读时间需要 4 分钟。

# -*- coding: utf-8 -*-#分析用户身份审核信息#python 3.5#xiaodeng#http://apistore.baidu.com/apiworks/servicedetail/113.htmlimport urllib.parseimport urllib.requestimport time#python UnicodeDecodeError: 'gbk' codec can't decode byte 0xff in position 0#解决以上编码错误问题#encoding= 'utf8'data=open("cardno.txt",encoding= 'utf8')result=open("result.txt","w",encoding= 'utf8')     #指定文件的编码格式url = "http://apis.baidu.com/apistore/idservice/id?id="for k in data:    k=k.strip()    k=k.split('\t')    uid=k[0]    name=k[1]    cardno=str(k[2])    print(cardno)    My_url=url+cardno    time.sleep(0.1)    try:        req = urllib.request.Request(My_url)        req.add_header("apikey","xxxxxxxxxxxxxxxxxxxxxxxxxxxxx")        response = urllib.request.urlopen(req)        the_page = response.read()        try:            the_page=eval(the_page)            try:                retData=the_page["retData"]                birthday=retData["birthday"]                sex=retData["sex"]                address=retData["address"]                result.write("%s\t%s\t%s"%(sex,birthday,address)+"\n")            except:                result.write("%s\t%s\t%s"%("数据错误","数据错误","数据错误")+"\n")        except Exception as err:            print(err)    except Exception as err:            print(err)result.close()
在打开和写入文件时,写明编码格式即可 encoding='utf8' data=open("cardno.txt",encoding= 'utf8')result=open("result.txt","w",encoding= 'utf8')     #指定文件的编码格式

 

转载于:https://www.cnblogs.com/dengyg200891/p/6059277.html

你可能感兴趣的文章
atitit 读书与获取知识资料的attilax的总结.docx
查看>>
B站 React教程笔记day2(3)React-Redux
查看>>
找了一个api管理工具
查看>>
Part 2 - Fundamentals(4-10)
查看>>
使用Postmark测试后端存储性能
查看>>
NSTextView 文字链接的定制化
查看>>
第五天站立会议内容
查看>>
ATMEGA16 IOport相关汇总
查看>>
JAVA基础-多线程
查看>>
面试题5:字符串替换空格
查看>>
[Codevs] 线段树练习5
查看>>
Amazon
查看>>
component-based scene model
查看>>
Echart输出图形
查看>>
hMailServer搭建简单邮件系统
查看>>
从零开始学习jQuery
查看>>
Spring+SpringMVC+MyBatis深入学习及搭建(四)——MyBatis输入映射与输出映射
查看>>
opacity半透明兼容ie8。。。。ie8半透明
查看>>
CDOJ_24 八球胜负
查看>>
Alpha 冲刺 (7/10)
查看>>