作者GoodByeLove (Fantastic!)
看板Python
标题[问题] for回圈读档处理
时间Tue May 13 17:08:56 2014
最近刚开始学Python, 想请问版上前辈几个问题
1. 有没有办法读档案後, 利用","做区分去读一个一个的string?
p.s. 我大致写了另一个在
http://codepad.org/P1b5A7dd
档案结构如同buf = "x503, 00x1, 60x2, x305, 503x, x503, 60x1,...."
想要的结果是符合x503的都会出现在result, 反之就在result2
2. 我以下的写法都只会比对第一个string, 後面的就都不会比了 @@
不知道问题出在哪? 希望大大指点一下
buf = open("input.txt", "rb")
ori = buf.read().strip().split(",")
temp = map(str, ori)
result = []
result2 = []
number = 0
for teststr in temp:
teststr = teststr.strip() #加上这行先把空白滤掉
if teststr != '':
if teststr == 'x503':
result.append(teststr)
number = number+1
else:
result2.append(teststr)
number = number+1
else:
break
print result
print result2
print number
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 39.14.172.234
※ 文章网址: http://webptt.com/cn.aspx?n=bbs/Python/M.1399972142.A.E07.html
1F:→ KSJ:没实作过 但看起来後面的503前面有个空格吧 像这样 " x503" 05/13 17:21
2F:→ math99:strip 应该用在 loop 里面 05/13 17:23
3F:→ GoodByeLove:感谢提醒。改了後正确罗!谢谢 05/13 18:06
※ 编辑: GoodByeLove (39.14.172.234), 05/13/2014 18:15:37