some python tips

一、WIN和Linux的路径表示不太一样,在两个平台分别使用os.getcwd()得到的结果分别是:”E:\PycharmProjects\untitled”,”/root”。WIN使用反斜杠\来分隔路径,而Linux使用/分隔。在使用到OS模块的时候,如果不是使用一些方法来动态获取路径的话,在移植的时候需要考虑到这个问题。
二、对于urllib模块,python3进行了一些修改。官方是这样说的:
a new urllib package was created. It consists of code from
urllib, urllib2, urlparse, and robotparser. The old
modules have all been removed. The new package has five submodules:
urllib.parse, urllib.request, urllib.response,
urllib.error, and urllib.robotparser. The
urllib.request.urlopen() function uses the url opener from
urllib2. (Note that the unittests have not been renamed for the
beta, but they will be renamed in the future.)
也就是把urllib2、urlparse、robotparser这几个模块移除后加入urllib,然后urllib多了5个子模块:urllib.parse, urllib.request, urllib.response,
urllib.error, and urllib.robotparser。

比如:

params = urllib.urlencode(params).encode(encoding='UTF8')#python2
params = urllib.parse.urlencode(params).encode(encoding='UTF8')#python3
ret = urllib.urlopen(uri, params)#python2
ret = urllib.request.urlopen(uri, params)#python3
三、IndentationError: unexpected indent
别说了,一定是你空格和tab又乱用了。(一些编辑器会自动把tab转化成八个空格,手残党就用它们吧)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值