相关疑难解决方法(0)

Python 2.7中的urllib.request

我可以在Python 3.1中使用urllib.request模块.但是当我使用Python 2.7执行相同的程序时,出现了一个错误:

AttributeError: 'module' object has no attribute 'request'.

我相信这个错误是因为在urllib中没有Python 2.7的请求模块.因为我需要使用tweepy,所以我必须坚持使用Python 2.7,因为tweepy不支持Python 3.

那么我如何在Python 2.7中使用urllib.request模块呢?

python twitter tweepy

20
推荐指数
2
解决办法
7万
查看次数

'ascii'编解码器不能编码位置*ord不在范围内的字符(128)

stackoverflow上有几个线程,但我找不到整个问题的有效解决方案.

我从urllib读取函数中收集了大量文本数据,并将其存储在pickle文件中.

现在我想将这些数据写入文件.写作时我得到的错误类似于 -

'ascii' codec can't encode character u'\u2019' in position 16: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)

而且很多数据正在丢失.

我想urllib读取的数据是字节数据

我试过了

   1. text=text.decode('ascii','ignore')
   2. s=filter(lambda x: x in string.printable, s)
   3. text=u''+text
      text=text.decode().encode('utf-8')
Run Code Online (Sandbox Code Playgroud)

但我仍然以类似的错误结束.有人可以指出一个合适的解决方案.并且编解码器也会剥离工作.如果冲突字节没有作为字符串写入文件,那么我就没有问题,因此可以接受丢失.

python unicode encode decode

10
推荐指数
2
解决办法
1万
查看次数

标签 统计

python ×2

decode ×1

encode ×1

tweepy ×1

twitter ×1

unicode ×1