![noavatar.png](https://img.php1.cn/3cd4a/1eebe/cd5/ff61bfdd3c0af92e.webp)
zeppe
if re.match('^\d', line):
#doWhatYouWant
else:
#this is the line doesn't start with number
![1124892.jpg](https://img.php1.cn/3cd4a/1eebe/cd5/8343fdbffb0056b5.webp)
seu-ljc
引用回帖:
zeppe at 2015-04-04 09:17:37
if re.match('^\d', line):
#doWhatYouWant
else:
#this is the line doesn't start with number
牛牛,这个难道不是判断此行有没有数字?不是判断此行前几位是不是数字开头吧?
请指教~
![2533501_1514875349.png](https://img.php1.cn/3cd4a/1eebe/cd5/433ea70d6ea577b1.jpeg)
me_yu
from pylab import *
import re
t=open('TH.txt','w')
fileHandle=open('1.txt','r')
line=fileHandle.readline()
while not('List' in line):
line=fileHandle.readline()
done = 0
while not done:
if line!='\n':
line=fileHandle.readline()
if line=='':
break
temp = ''
for i in line:
if i.isdigit() or i ==' ' or i == '\n':#每行拆分到每个字符判断就好了,不是数字的就排除
temp+=i#排除非数字后再重新组合
t.write(temp)
else:
done=1
fileHandle.close()
t.close()
![1124892.jpg](https://img.php1.cn/3cd4a/1eebe/cd5/8343fdbffb0056b5.webp)
seu-ljc
引用回帖:
me_yu at 2015-04-04 21:32:37
from pylab import *
import re
t=open('TH.txt','w')
fileHandle=open('1.txt','r')
line=fileHandle.readline()
while not('List' in line):
line=fileHandle.readline()
done = 0
while not done: ...
牛牛,按你的代码得出来的结果如下:
0287286028902920296028203170205082355
1287286028902920296028203170292082356
2287286028902920296028203180471082357
3288286028902920296028203180445082358
4288286028902920296028203180377082359
5288286028902920296028203180284082400
6287286028902920296028203170273082401
。。。
空格没有了。。。想要的结果是
0 28.7 28.60 28.90 29.20 29.60 28.20 31.70 20.50 8 23 55
1 28.7 28.60 28.90 29.20 29.60 28.20 31.70 29.20 8 23 56
2 28.7 28.60 28.90 29.20 29.60 28.20 31.80 47.10 8 23 57
3 28.8 28.60 28.90 29.20 29.60 28.20 31.80 44.50 8 23 58
4 28.8 28.60 28.90 29.20 29.60 28.20 31.80 37.70 8 23 59
5 28.8 28.60 28.90 29.20 29.60 28.20 31.80 28.40 8 24 00
还望送佛送到西~计算机大牛给的思路确实�
,
![1124892.jpg](https://img.php1.cn/3cd4a/1eebe/cd5/8343fdbffb0056b5.webp)
seu-ljc
引用回帖:
me_yu at 2015-04-05 00:08:21
if i.isdigit() or i ==' ' or i == '\n':#要保留什么字符在这里加进去就好了 比如还有点(.)和换行符就这样再加一个 i == '.' or i == '\n'
...
经你点拨,我用了此句if i.isdigit() or i =='\t' or i == '.' or i=='\n':
得到结果如下:
0 28.7 28.60 28.90 29.20 29.60 28.20 31.70 20.50 82355
后面的82355并没有分成 8 23 55?
我之前的方法temperaturetime=re.findall(r'\d*\.?\d+',line)可以实现结果8 23 55,可惜不能实现判定字符串的前几个字符是不是数字?
所以不知完全按你的思路,如何把82355 分开,后面要对这些数字进行处理,故而必须要将82355 分开~
![2533501_1514875349.png](https://img.php1.cn/3cd4a/1eebe/cd5/433ea70d6ea577b1.jpeg)
me_yu
引用回帖:
seu-ljc at 2015-04-05 00:18:27
经你点拨,我用了此句if i.isdigit() or i =='\t' or i == '.' or i=='\n':
得到结果如下:
0 28.7 28.60 28.90 29.20 29.60 28.20 31.70 20.50 82355
后面的82355并没有分成 8 23 55?
我之前的方 ...
#稍微动下脑筋就可以解决的啊,代码如下:
from pylab import *
import re
t=open('TH.txt','w')
fileHandle=open('1.txt','r')
line=fileHandle.readline()
while not('List' in line):
line=fileHandle.readline()
done = 0
while not done:
if line!='\n':
line=fileHandle.readline()
if line=='':
break
temp = ''
temperaturetime=re.findall(r'\d*\.?\d+',line)
line = line.replace(':',' ')#将冒号替换成空格
for i in line:
if i.isdigit() or i ==' ' or i == '\n' or i == '.' or i ==' ':#将判断冒号条件改为空格条件
temp+=i
t.write(temp)
else:
done=1
fileHandle.close()
t.close()