python读文本处理小结一下!

output.txt 内容
helo:word
qwe:rtgbh52800op
poil:7851fghjj874

file=open("output.txt")
lines=file.read()
for str in line.split("\n"):
    word=str.split(":")
    print word[0]

打印以“:”间隔的第一列的数据!


string 模块测试
>>>string.atof("12.2")
12.199999999999999

>>> s = 'mary had a little lamb'
";".join(string.split(s))

把字典查分成列表
>>> params={"aaa":"ddd","ccc":"fdsa"}
>>> params.items()

#!/usr/bin/python
# -*- coding: utf-8 -*-
# File: eyouadd.py
import sys
#file = open("test.txt")
filename=sys.argv[1]
file = open(filename)      
line = file.read()
for str in line.split("\n"):
word = str.split(":")
print "mod {"
print "uid=%s" %  word[0]
print "domain=eyou.com"
print "userPassord=%s" % word[1]
print "}"
print "."
file.close()
print "exit"
if __name__ == "__main__":
  print "test"