24小时热门版块排行榜    

北京石油化工学院2026年研究生招生接收调剂公告
查看: 1714  |  回复: 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的回帖

me_yu

木虫 (正式写手)

引用回帖:
7楼: Originally posted by 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()

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

听,寂静的夜里,田野里蛙声和虫鸣,好不欢快!
8楼2015-04-05 09:13:31
已阅   回复此楼   关注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的回帖

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的回帖

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的回帖
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[考研] 317求调剂 +7 蛋黄咸肉粽 2026-03-26 7/350 2026-03-27 02:29 by fmesaito
[考研] 一志愿郑州大学,080500学硕,总分317分求调剂 +4 举个栗子oi 2026-03-24 5/250 2026-03-26 23:15 by 不吃魚的貓
[考研] 349求调剂 +4 李木子啊哈哈 2026-03-25 4/200 2026-03-26 22:49 by fmesaito
[考研] 321求调剂 +6 Ymlll 2026-03-24 6/300 2026-03-26 20:50 by 不吃魚的貓
[考研] 【双一流院校新能源、环境材料,材料加工与模拟招收大量调剂】 +4 Higraduate 2026-03-22 8/400 2026-03-26 20:34 by Higraduate
[考研] 081200-11408-276学硕求调剂 +3 崔wj 2026-03-26 3/150 2026-03-26 19:57 by nihaoar
[考研] 266分求材料化工冶金矿业等专业的调剂 +3 哇呼哼呼哼 2026-03-26 3/150 2026-03-26 19:16 by JourneyLucky
[考研] 263求调剂 +6 yqdszhdap- 2026-03-22 10/500 2026-03-26 13:11 by 公瑾逍遥
[考研] 299求调剂 +4 15188958825 2026-03-25 4/200 2026-03-25 22:56 by 418490947
[考研] 332求调剂 +6 032500 2026-03-25 6/300 2026-03-25 22:45 by 418490947
[考研] 化学调剂 +6 yzysaa 2026-03-21 6/300 2026-03-25 09:27 by aa331100
[考研] 318求调剂 +5 plum李子 2026-03-21 8/400 2026-03-25 09:26 by aa331100
[考研] 311求调剂 +3 冬十三 2026-03-24 3/150 2026-03-24 21:31 by peike
[考研] 食品专硕 一志愿双一流 328 +3 xiaom99 2026-03-21 4/200 2026-03-24 21:20 by lailaisimei
[考研] 材料专硕331求调剂 +4 鲜当牛 2026-03-24 4/200 2026-03-24 15:58 by JourneyLucky
[考研] 求调剂一志愿武汉理工大学材料工程(085601) +5 WW.' 2026-03-23 7/350 2026-03-24 14:50 by sprinining
[考研] 一志愿重庆大学085700资源与环境,总分308求调剂 +7 墨墨漠 2026-03-23 8/400 2026-03-23 20:36 by Creta
[考研] 336求调剂 +4 收到VS 2026-03-20 4/200 2026-03-23 19:02 by macy2011
[考研] 一志愿华中农业071010,总分320求调剂 +5 困困困困坤坤 2026-03-20 6/300 2026-03-22 17:41 by hxsm
[考研] 求调剂院校信息 +6 CX 330 2026-03-21 6/300 2026-03-22 15:25 by 无懈可击111
信息提示
请填处理意见