收集: python中列表,元组,字符串 相互转换, IO 数据处理,行数据切割

出处 http://blog.csdn.net/sruru/article/details/7803208

一: 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']

tuple expression To string 

>>> str(tuple(s))
"('x', 'x', 'x', 'x', 'x')"

list expression To string

>>> str(list(s))
"('x', 'x', 'x', 'x', 'x')"

列表和元组 [值] 转换为字符串则必须依靠join函数:

>>> "".join(tuple(s))
'xxxxx'
>>> "".join(list(s))
'xxxxx'

二: 读取文件获得一行数据,将其按单词划分开,各单词直接空格数不确定: split()

 f = open(FILE_NAME, 'r')

 for line in f.readlines():

      s_lt = line.split()

 ...

f.close()

三: 远程连接读取终端信息事例:

read_until(), splitlines()
def telnet_remote2():
    tel_obj = telnet_connect()
    rst_list = []
    for s in pid_lst:
        tel_obj.write("showmem -P " +s+"\n")
        pidin_info = tel_obj.read_until("#")
        info_list = pidin_info.splitlines()
        rst_list =  rst_list + info_list   
    tel_obj.write("exit\n")

    return (rst_list)
原文地址:https://www.cnblogs.com/xiaoxxy/p/3115764.html