出现该错误往往是通过open()函数打开文本文件时,使用了‘rb’属性,如:fileHandle=open(filename,'rb'),则此时是通过二进制方式打开文件的,所以在后面处理时如果使用了str()函数,就会出现该错误,该错误不会再python2中出现。

具体解决方法有以下两种:

第一种,在open()函数中使用‘r’属性,即文本方式读取,而不是‘rb’,以二进制文件方式读取,可以直接解决问题。

第二种,在open()函数中使用‘rb’,可以在使用之前进行转换,有以下实例,来自:http://stackoverflow.com/questions/33054527/python-3-5-typeerror-a-bytes-like-object-is-required-not-str

1:查找文件时:
with open(fname, 'rb') as f:
if b'some-pattern' in tmp: continue 2:使用socketl连接时:
mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysock.connect(('www.py4inf.com', 80))
mysock.send(**b**'GET http://www.py4inf.com/code/romeo.txt HTTP/1.0\n\n') while True:
data = mysock.recv(512)
if ( len(data) < 1 ) :
break
print (data); mysock.close() 3:提前进行编码:
with open(fname, 'rb') as f:
lines = [x.decode('utf8').strip() for x in f.readlines()]

python 3.5: TypeError: a bytes-like object is required, not 'str'的更多相关文章

  1. 【爬坑】Python 3.6 在 Socket 编程时出现类型错误 TypeError: a bytes-like object is required, not 'str'

    1. 问题描述 Python 3.6 在 Socket 编程时出现错误如下 Traceback (most recent call last): File "F:/share/IdeaPro ...

  2. python问题:TypeError: a bytes-like object is required, not 'str'

    源程序: import socket target_host = "www.baidu.com" # 127.0.0.1 target_port = 80 # 建立一个socket ...

  3. python3 TypeError: a bytes-like object is required, not 'str'

    在学习<Python web开发学习实录>时, 例11-1: # !/usr/bin/env python # coding=utf-8 import socket sock = sock ...

  4. TCP客户端和服务器间传输数据遇到的TypeError: a bytes-like object is required, not 'str'问题

    使用python实现python核心编程3第472页和474页的TCP时间戳服务器和客户端服务器间数据传输编程时遇到TypeError: a bytes-like object is required ...

  5. moviepy执行TextClip.search方法时报错TypeError: a bytes-like object is required, not str

    ☞ ░ 前往老猿Python博文目录 ░ 执行TextClip.search方法时,报错: >>> from moviepy.editor import * >>> ...

  6. linux下运行python3出现TypeError: a bytes-like object is required, not 'str'

    目标:用python将中文存入csv,且中文正常显示. 环境:linux,python3 百度N久,方法都不行或是比较复杂. 以上代码用python3运行后,出现TypeError: a bytes- ...

  7. sbt package报错:a bytes-like object is required, not 'str'

    Traceback (most recent call last): File , in <module> s.sendall(content) TypeError: a bytes-li ...

  8. python中MySQL模块TypeError: %d format: a number is required, not str异常解决

    转载自:http://www.codeif.com/topic/896 python代码: attr_sql = "INSERT INTO `ym_attribute` (`attr_nam ...

  9. 【Python】TypeError: a bytes-like object is required, not 'str'解决

    对所使用的字符串类型调用encode()方法进行转换即可

随机推荐

  1. php use memcached in ubuntu 14.04

    I assume you already had a lamp environment first step,we must to install memched in our Ubuntu Syst ...

  2. com.panie 项目开发随笔_前后端框架考虑(2016.12.8)

    (一) 近日和一同学联系,说了我想要做一个网站的打算.她很感兴趣.于是我们协商了下,便觉得一起合作.她写前端,我写后台.因为我对于前端样式设计并不怎么熟悉. (二) 我们决定先做一个 个人博客. 网上 ...

  3. android studio 提示翻译

    1. you can import your settings from a previous version of Studio可以导入您的设置从先前版本的工作室 2. I want to impo ...

  4. css3 背景渐变

    本来想先瞎扯些什么,然后又不知道讲什么的好,那就直接进入正题吧. 参考资料: Using CSS gradients  (以及该页面内的大部分链接页面的资料) 首先区分下,平常给的纯色backgrou ...

  5. Python迭代器,可迭代对象,生成器

    迭代器 迭代器(iterator)有时又称游标(cursor)是程式设计的软件设计模式,可在容器物件(container,例如链表或阵列)上遍访的界面,设计人员无需关心容器物件的内存分配的实现细节. ...

  6. ios 快速审核

    https://developer.apple.com/contact/app-store/?topic=expedite

  7. js键盘事件和焦点事件

    键盘事件onkeydown //当键盘按下的时候触发onkeyup //但键盘抬起的时候触发event.keyCode //数字类型 键盘按键的键值功能键 ctrlkey shiftkey altke ...

  8. MediaElement 的两种模式

    MediaElement 是一个 UIElement,它受 布局系统 支持并可用作许多控件的内容.它也可用在可扩展应用程序标记语言 (XAML) 以及代码中.另一方面,MediaPlayer 用于 D ...

  9. UML大战需求分析--阅读笔记02

    这次阅读了第三章--类图.本章主要讲解了类图的基本使用规则和一些使用的例子.类图是UML中非常重要的一部分,作用很大. 类图之间有五种关系:关联关系,聚合关系,组合关系,泛化关系,依赖关系.关联关系有 ...

  10. [Java] ApplicationContext 辅助类

    我们经常需要获取各种 bean , 需要用到 context. 下面的类可以方便的使用 context , 获取 bean 等. import java.io.File; import java.ut ...