24小时热门版块排行榜    

Znn3bq.jpeg
汕头大学海洋科学接受调剂
查看: 1761  |  回复: 10
当前只显示满足指定条件的回帖,点击这里查看本话题的所有回帖

seu-ljc

木虫 (正式写手)

[求助] python如何判定字符串的前几个字符是不是数字?

python如何判定字符串的前几个字符是不是数字?
捕获.JPG

如上图是一个txt的内容,楼主想要提取txt文件中的每一行数据
可是txt有的行里面不是想要的数据,楼主想要剔除这个行,楼主想请教各位达人,如何判定每一行的字符串的前几个字符是不是数字?

请诸位牛牛不吝赐教~

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
                #line = [ float( line ) for line in line if line ]
                temperaturetime=re.findall(r'\d*\.?\d+',line)
                print(temperaturetime[1])

                t.write('%f\n'% (float(temperaturetime[1])))
                #force1.append(float(force[1]))
        else:
                done=1



fileHandle.close()
t.close()
回复此楼

» 本帖附件资源列表

  • 欢迎监督和反馈:小木虫仅提供交流平台,不对该内容负责。
    本内容由用户自主发布,如果其内容涉及到知识产权问题,其责任在于用户本人,如对版权有异议,请联系邮箱:xiaomuchong@tal.com
  • 附件 1 : 1.txt
  • 2015-04-03 21:45:33, 240.44 K

» 收录本帖的淘帖专辑推荐

source

» 猜你喜欢

» 本主题相关价值贴推荐,对您同样有帮助:

已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

seu-ljc

木虫 (正式写手)

引用回帖:
2楼: Originally posted by zeppe at 2015-04-04 09:17:37
if re.match('^\d', line):
   #doWhatYouWant
else:
   #this is the line doesn't start with number

牛牛,这个难道不是判断此行有没有数字?不是判断此行前几位是不是数字开头吧?
请指教~
3楼2015-04-04 10:36:02
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
查看全部 11 个回答

zeppe

金虫 (小有名气)

【答案】应助回帖

★ ★ ★ ★ ★ ★ ★ ★ ★ ★
感谢参与,应助指数 +1
seu-ljc: 金币+5, ★★★很有帮助 2015-04-04 10:36:42
seu-ljc: 金币+5, ★★★很有帮助 2015-04-05 11:54:25
if re.match('^\d', line):
   #doWhatYouWant
else:
   #this is the line doesn't start with number
2楼2015-04-04 09:17:37
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

me_yu

木虫 (正式写手)

【答案】应助回帖

★ ★ ★ ★ ★ ★ ★ ★ ★ ★
感谢参与,应助指数 +1
seu-ljc: 金币+10, ★★★很有帮助, 感谢牛牛的参与 2015-04-04 23:47:34
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()

» 本帖已获得的红花(最新10朵)

听,寂静的夜里,田野里蛙声和虫鸣,好不欢快!
4楼2015-04-04 21:32:37
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

seu-ljc

木虫 (正式写手)

送红花一朵
引用回帖:
4楼: Originally posted by 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       

还望送佛送到西~计算机大牛给的思路确实牛
5楼2015-04-04 23:50:25
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[考研] 085600材料与化工349分求调剂 +16 李木子啊哈哈 2026-04-12 17/850 2026-04-14 09:11 by fenglj492
[考研] 279学硕食品专业求调剂院校 20+6 孤独的狼爱吃羊 2026-04-12 26/1300 2026-04-14 09:04 by zs92450
[考研] 求调剂学校 +6 不会吃肉 2026-04-13 6/300 2026-04-14 08:36 by 木木mumu~
[考研] 考研求调剂 +12 子木呐 2026-04-12 13/650 2026-04-14 01:19 by 王珺璞
[考研] 材料考研调剂 +29 云木达达 2026-04-11 31/1550 2026-04-13 13:32 by lyh鲁老师
[考研] 22408 352分求调剂 +5 努力的夏末 2026-04-09 5/250 2026-04-12 19:17 by wj165256
[考研] 求调剂,262机械专硕 +8 嗯yyl 2026-04-08 8/400 2026-04-12 02:31 by 秋豆菜芽
[考研] 343求调剂 +9 王国帅 2026-04-10 9/450 2026-04-11 20:31 by dongdian1
[考研] 352 求调剂 +6 yzion 2026-04-11 8/400 2026-04-11 16:24 by 明月此时有
[考研] 085410-273求调剂 +6 X1999 2026-04-10 6/300 2026-04-11 10:32 by Delta2012
[考研] 283求调剂,工科! +12 苏打水7777 2026-04-08 12/600 2026-04-11 10:28 by 逆水乘风
[考研] 337求调剂 +4 研s. 2026-04-10 4/200 2026-04-11 08:57 by zhq0425
[考研] 调剂 +12 卷卷卷心菜_ 2026-04-09 13/650 2026-04-10 22:36 by Ftglcn90
[考研] 中科院总分315求调剂 +8 lallalh 2026-04-09 8/400 2026-04-10 19:30 by dick_runner
[考研] 求调剂 +5 不会飞的鱼@ 2026-04-10 5/250 2026-04-10 19:07 by chemisry
[考研] 314求调剂 +18 xhhdjdjsjks 2026-04-09 19/950 2026-04-10 18:53 by HPUCZ
[考研] 江苏大学 工科调剂 捡漏 +3 Evan_Liu 2026-04-09 5/250 2026-04-10 10:22 by Evan_Liu
[考研] 314求调剂 +14 weltZeng 2026-04-09 14/700 2026-04-09 23:14 by wolf97
[论文投稿] 求助文献原文 10+3 18500821399 2026-04-08 3/150 2026-04-09 16:56 by 北京莱茵润色
[考研] 一志愿武理车辆 281 求调剂 +5 上岸研究生. 2026-04-07 5/250 2026-04-09 15:56 by only周
信息提示
请填处理意见