python 常用操作

python 常用操作

实际应用

https://gitee.com/lohas5669/logParser

pycharm 配置gitlab

http://www.cnblogs.com/caseast/p/6085837.html

Python命令行参数

Python 提供了 getopt 模块来获取命令行参数,与C语言类似。

$ python test.py arg1 arg2 arg3

Python 中也可以所用 syssys.argv 来获取命令行参数:

  • sys.argv 是命令行参数列表。
  • len(sys.argv) 是命令行参数个数。

**注:**sys.argv[0] 表示脚本名。

  • 参数个数为:4个
  • 从0开始计数
sys.argv[0] = 'test.py'
sys.argv[3] = 'arg3'

Python文件操作

http://blog.51cto.com/lizhenliang/1874018

https://datartisan.gitbooks.io/begining-text-mining-with-python/content/%E7%AC%AC3%E7%AB%A0%20%E6%96%87%E6%9C%AC%E6%95%B0%E6%8D%AE%E6%9D%A5%E6%BA%90/3.4%20%E8%AF%BB%E5%86%99%E6%96%87%E6%9C%AC%E6%95%B0%E6%8D%AE.html

open函数

使用方法

f = open( file , mode)

常用参数
ModeDescription
r只读,默认
w只写,打开前清空文件内容
a追加
a+读写,写到文件末尾
w+可读写,清空文件内容
r+可读写,能写到文件任何位置
rb二进制模式读
wb二进制模式写,清空文件内容

读文件

  • read()
    • 读取所有内容
    • 可以按照字节读取
  • readline()
    • 读取下一行内容
  • readlines()
    • 读取所有行,返回一个list

写文件

  • write()
    • 写字符串到文件
  • writelines()
    • 写一个list到文件

其他常用函数

  • close()
    • 关闭文件
  • tell()
    • 返回当前位置
  • seek()
    • 从指定位置读取
  • flush()
    • 刷新文件 ,从内存中写入磁盘里

字符串

slice函数

slice() 函数实现切片对象,主要用在切片操作函数里的参数传递。

slice 语法:

class slice(stop)
class slice(start, stop[, step])

参数说明:

  • start – 起始位置
  • stop – 结束位置
  • step – 间距

返回一个切片对象。

http://www.runoob.com/python/python-func-slice.html

split函数

Python split() 通过指定分隔符对字符串进行切片,如果参数 num 有指定值,则仅分隔 num 个子字符串

语法

split() 方法语法:

str.split(str="", num=string.count(str)).
参数
  • str – 分隔符,默认为所有的空字符,包括空格、换行(\n)、制表符(\t)等。
  • num – 分割次数。

列表

列表是Python中比较常用的数据结构,其内容可以是任意类型,字符串,字典,列表等等,通过类似数组的索引或者下标进行访问。

http://www.runoob.com/python/python-lists.html

列表、元组、字符串、字典的相互转换

  • 字符串 转 列表、元组

    •   python中有三个内建函数:列表,元组和字符串,他们之间的互相转换使用三个函数,str(),tuple()和list(),

    具体示例如下所示:

    s = "xxxxx" 
    >>> list(s) 
    ['x', 'x', 'x', 'x', 'x'] 
    >>> tuple(s) 
    ('x', 'x', 'x', 'x', 'x') 
    >>> tuple(list(s)) 
    ('x', 'x', 'x', 'x', 'x')
    >>> list(tuple(s)) 
    ['x', 'x', 'x', 'x', 'x']
    • split函数
  • 列表 转 字符串

    • join函数
    • 或者每个元素用‘+’连接
  • 列表转字典

    • dict(list)
    
    #method 1
    
      dict_op = dict(operand_list)
    
    # Convert to dict (method 2)
    
     for op in operand_list:
        dict_op[op[0]] = op[1]

https://blog.csdn.net/sruru/article/details/7803208

分片

接下来来看一个实际的需求。假设有如下列表:

['one', 1, 'two', 2, 'three', 3, 'four', 4, 'five', 5]

现在我们要创建一个字典,并将以上列表中的元素交替作为键和值。利用刚才分片的技巧可以很方便的做到:

>>> l_odd = l[::2]
>>> l_even = l[1::2]
>>> l_odd
['one', 'two', 'three', 'four', 'five']
>>> l_even
[1, 2, 3, 4, 5]
>>> d = dict(zip(l_odd, l_even))
>>> d
{'four': 4, 'three': 3, 'five': 5, 'two': 2, 'one': 1}

Strip() 去除首尾无用字符

Python strip() 方法用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列。

注意:该方法只能删除开头或是结尾的字符,不能删除中间部分的字符。

str2 = "   Runoob      ";   # 去除首尾空格
print str2.strip();
str = "123abcrunoob321"
print (str.strip( '12' ))  # 字符序列为 12
#以上实例输出结果如下:
3abcrunoob3

http://www.runoob.com/python/att-string-strip.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值