求助python的字符串处理
需求:
文件a.txt内容如下
>> cat a.txt
/* ME Type=DEC, Version=V100R003C01*/
/* ME Type=NOV, Version=V100R003C00*/
/* ME Type=CCVS, Version=V100R003C03*/
/* ME Type=OCTOB, Version=V100R003C08*/
请问如何使用python脚本获取文件中的ME type,使之对数组a赋值:
a1=DEC
a2=NOV
a3=CCVS
a4=OCTOB
re.match
大侠,请帮忙写一个简单例子,刚开始学python,谢谢!
高级点这样:
Python code:
import re
reg = re.compile(r'Type=(\w*),')
def GetType(str):
match = reg.search(str)
if match is not None:
return match.groups(0)[0]
f = open('a.txt')
a = map(GetType, f)
print a
f.close()
当然如果你的数据相当规范,一定是以Type=为前导,也可以不用正则:
Python code:
import re
prefix = 'Type='
def GetType(str):
for i in str.split():
if i.startswith(prefix):
return i[len(prefix):-1]
f = open('a.txt')
a = map(GetType, f)
print a
f.close()
这只是举个例子,你对数据越了解,处理手段就越多。
re.findall('''Type=(.*?),''', str)
import re
f = open('t.txt')
相关问答:
已知Python 中:
s = unicode("测试", "gb2312")
s = u'\u6d4b\u8bd5'
print s
测试
在Delphi里面如何将\u6d4b\u8bd5这样的还原成Gb2312的汉字呢?
找到个方法
......
像 C 的 scanf() 那样
比如读入 1 2 3 a b c
每次读入一个
下面这个应该是你想要的吧:
Python code:
>>> k = raw_input()
0 0123 ds dsl sd
>>> k
'0 0123 ds dsl sd'
>>> ......
s='aaa111aaa,bbb222,333ccc,444ddd444,555eee666,fff777ggg'
用正则表达式取出 前后字母相同的数据 结果如下:
111 ddd
谢谢~
Python code:
import re
s='aaa111aaa,bbb222,333ccc,444ddd444,555eee666,ff ......
feedparser导入这个包 读xml的title,link 都没问题 就是时间读取不出来
错误:
print d['feed']['%s lastBuildDate']
return UserDict.__getitem__(self, realkey)
KeyError: ' ......
想通过python post来实现自动登录新浪围脖已获得登录后页面的源码
但是仔细的看了下登录页面的源码
HTML code:
<body style=" background:#C4EFF4 url(http://simg.sinajs.cn/miniblog/images/commo ......