• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > 收集的几个Python小技巧分享

收集的几个Python小技巧分享

作者:junjie 字体:[增加 减小] 来源:互联网

junjie 通过本文主要向大家介绍了教育资源收集技巧,网络信息收集的技巧有,蚂蚁森林收集能量技巧,刷阳光收集瓶技巧,福卡收集技巧等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

获得当前机器的名字:

def hostname():
        sys = os.name 
 
        if sys == 'nt': 
                hostname = os.getenv('computername') 
                return hostname 
 
        elif sys == 'posix': 
                host = os.popen('echo $HOSTNAME') 
                try: 
                        hostname = host.read() 
                        return hostname 
                finally: 
                        host.close()
        else: 
                return 'Unkwon hostname'
</div>

获取当前工作路径:

import os
 
os.getcwd()

#or

#os.curdir just return . for current working directory.
#need abspath() to get full path.
os.path.abspath(os.curdir)
</div>

获取系统的临时目录:

os.getenv('TEMP')
</div>

字符串与int,long,float的转化:

python的变量看起来是没有类型的,其实是有变量是有类型的。

使用locale模块下的atoi和atof来将字符串转化为int或float,或者也可以直接使用int(),float(),str()来转化。以前的版本中atoi和atof是在string模块下的。

s = "1233423423423423"
import locale
locale.atoi(s)
#1233423423423423
locale.atof(s)
#1233423423423423.0
int(s)
#1233423423423423
float(s)
#1233423423423423.0
str(123434)
"123434"
</div>

bytes和unicodestr的转化:

# bytes object 
 b = b"example" 
 
 # str object 
 s = "example" 
 
 # str to bytes 
 bytes(s, encoding = "utf8") 
 
 # bytes to str 
 str(b, encoding = "utf-8") 
 
 # an alternative method 
 # str to bytes 
 str.encode(s) 
 
 # bytes to str 
 bytes.decode(b)
</div>

写平台独立的代码必须使用的:


>>> import os
>>> os.pathsep
';'
>>> os.sep
'\\'
>>> os.linesep
'\r\n'
</div>

</div>

您可能想查找下面的文章:

  • 收集的几个Python小技巧分享
  • 收集的几个Python小技巧分享

相关文章

  • Python中编写ORM框架的入门指引
  • Python中模块与包有相同名字的处理方法
  • 利用numpy+matplotlib绘图的基本操作教程
  • Python模仿POST提交HTTP数据及使用Cookie值的方法
  • python中ConfigParse模块的用法
  • 理解Python垃圾回收机制
  • Python3遍历目录树实现方法
  • Python实现网络端口转发和重定向的方法
  • django之常用命令详解
  • 在Python的Django框架中simple-todo工具的简单使用

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • Python函数嵌套实例
    • Python编程实现删除VC临时文件及Debug目录的方法
    • python获取指定时间差的时间实例详解
    • Python制作Windows系统服务
    • Python查询Mysql时返回字典结构的代码
    • python用于url解码和中文解析的小脚本(python url decoder)
    • Python中的日期时间处理详解
    • python 与GO中操作slice,list的方式实例代码
    • python实现简单爬虫功能的示例
    • python代码检查工具pylint 让你的python更规范

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有