Python中split()函数,通常用于将字符串切片并转换为列表。

一、函数说明:

split():语法:str.split(str="",num=string.count(str))[n]

拆分字符串。通过制定分隔符将字符串进行切片,并返回分割后的字符串列表[list]

参数:str:分隔符,默认为空格,但不能为空("")

   num: 表示分割次数。如果指定num,则分割成n+1个子字符串,并可将每个字符串赋给新的变量

   [n]: 选取第n个分片,即第n个字符串,从0开始算。

二、示例:

   u = "www.google.com"

   print u.split('.') --> ['www','google','com']

   print u.split('.',1)-->['www','google.com']

   print u.split('.',2)[1]-->google

   u1,u2,u3 = u.split('.')

三、实际应用:

写了一个读取ip列表,并自动替换掉配置文件里host内容的python脚本。

ip_list.txt 内容:(机房名称和IP之间用空格,详见下文spilt参数)

吉林枢纽联通-01 122.0.0.1

吉林枢纽联通-01 122.0.0.2

"hostname": "吉林枢纽联通-01  122.0.0.1",

 def rep_hostname():

     ip_dict = {}
ip_local = []
flag = 0 with open('ip_list.txt') as f:
for line in f.readlines():
line = line.strip('\n').split(' ')
ip_dict[line[1]] = line[0].decode('GBK') ips = os.popen("ifconfig|grep \"inet addr:\"|head -2 |awk '{print $2}'|cut -d':' -f2") for i in ips.readlines():
ip = i.strip('\n')
if ip in ip_dict:
flag = 1
print "The Right IP is : %s" % ip
(stat, out) = commands.getstatusoutput('sed -i "s#\\\"hostname\\\": \\\"\\\",#\\\"hostname\\\": \\\"%s %s\\\",#g" /home/langxiaowei/falcon-agent/cfg.json' % (ip_dict[ip],ip))
if stat != 0 :
print out
return
ip_local.append(ip) if flag == 0 :
print "Error: %s not in ip_list!" % ip_local return flag if __name__ == '__main__':
32    rep_hostname()

Python中split()函数的用法及实际使用示例的更多相关文章

  1. Python中int()函数的用法浅析

      int()是Python的一个内部函数 Python系统帮助里面是这么说的 >>> help(int)  Help on class int in module __builti ...

  2. python中split函数的使用

    最近学习python,对split函数做了下总结,内容如下:

  3. python中range()函数的用法

    python中range()函数可创建一个整数列表,一般用在for循环中. range()函数语法: range(start,stop[,step]) 参数说明: star: 计数从star开始.默认 ...

  4. Linux里AWK中split函数的用法

    跟java里的split函数的用法是很相像的,举例如下: The awk function split(s,a,sep) splits a string s into an awk array a u ...

  5. python中strip函数的用法

    python中往往使用剥除函数strip()来对用户的输入进行清理.strip函数的最一般形式为: str.strip('序列') 其中,序列是一段字符串,该函数表示从头或者从尾部开始进行扫描,如果扫 ...

  6. [转].Python中sorted函数的用法

    [Python] sorted函数 我们需要对List.Dict进行排序,Python提供了两个方法对给定的List L进行排序,方法1.用List的成员函数sort进行排序,在本地进行排序,不返回副 ...

  7. python中open函数的用法

    用法如下: name = open('errname.txt','w')name.readline()name.close() 1.看下第一行的代码 用来访问磁盘中存放的文件,可以进行读写等操作,例如 ...

  8. Python中sorted函数的用法(转)

    [Python] sorted函数 我们需要对List.Dict进行排序,Python提供了两个方法 对给定的List L进行排序, 方法1.用List的成员函数sort进行排序,在本地进行排序,不返 ...

  9. Python中print()函数的用法详情

    描述 print() 方法用于打印输出,最python中常见的一个函数. 在交互环境中输入help(print)指令,可以显示print()函数的使用方法. >>> help(pri ...

随机推荐

  1. PHP太怪了,in_array() ,strpos,

    PHP中在某个字符中查找另外一个字符串,是否存在,用的是strpos,此函数用法,经常很多人用反了,正确的用法是strpos(string,search),strstr等,前面是原字符串,后面是要在原 ...

  2. UVA-12436 Rip Van Winkle's Code (线段树区间更新)

    题目大意:一个数组,四种操作: long long data[250001]; void A( int st, int nd ) { for( int i = st; i <= nd; i++ ...

  3. kuangbin_UnionFind B (POJ 1611)

    过程是模板 merge完后扫一下几个跟0同祖先节点就是答案了 #include <iostream> #include <string> #include <cstdio ...

  4. Android——不同activity之间数据传递

    /* * 不同activity之间数据的传递 */ public class MainActivity extends Activity { private EditText et_name; @Ov ...

  5. unbuntu下安装flash插件

    adobe flash player的官方下载页面为:https://get.adobe.com/cn/flashplayer/ 不过近期通过APT方式以及ubuntu的软件中心都安装不了flashp ...

  6. debugging tools

    https://blogs.msdn.microsoft.com/debugdiag/ https://blogs.msdn.microsoft.com/debuggingtoolbox/2012/1 ...

  7. C#中 ()=>的含义

    这是 .NET3.0以后的新特性 Lambda表达式 RelayCommand(() => this.AddPerson(), () => this.CanAddPerson()); 的意 ...

  8. 自定义android程序一段时间无操作后的功能

    项目中遇见一个这样的需求,就是当软件在一定时间没有操作时候需要弹出广告页面,当点击广告页面时又进行软件操作,也就是广告要在软件打开并且处于未操作状态才会出来. 方法一:用handler+onTouch ...

  9. 关于C# 中的Attribute 特性

    关于C# 中的Attribute 特性 作者: 钢钢  来源: 博客园  发布时间: 2011-01-09 23:30  阅读: 13921 次  推荐: 12   原文链接 [收藏] 摘要:纠结地说 ...

  10. 在Linux中搭建一个FTP服务器

    在Linux中搭建一个ftp服务器,以供两个工作小组保管文件使用.禁用匿名.第一个小组使用ftp账号:ftp1,工作目录在:/var/ftp/ftp1:第二个小组使用ftp2,工作目录在:/var/f ...