| 查看: 1721 | 回復: 10 | ||
[求助]
python如何判定字符串的前幾個字符是不是數(shù)字?
|
||
source |
金蟲 (小有名氣)
金蟲 (小有名氣)
木蟲 (正式寫手)
|
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':#每行拆分到每個字符判斷就好了,不是數(shù)字的就排除 temp+=i#排除非數(shù)字后再重新組合 t.write(temp) else: done=1 fileHandle.close() t.close() |

送紅花一朵 |
牛牛,按你的代碼得出來的結(jié)果如下: 0287286028902920296028203170205082355 1287286028902920296028203170292082356 2287286028902920296028203180471082357 3288286028902920296028203180445082358 4288286028902920296028203180377082359 5288286028902920296028203180284082400 6287286028902920296028203170273082401 。。。 空格沒有了。。。想要的結(jié)果是 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 還望送佛送到西~計算機大牛給的思路確實牛 |
木蟲 (正式寫手)

送紅花一朵 |
經(jīng)你點撥,我用了此句if i.isdigit() or i =='\t' or i == '.' or i=='\n': 得到結(jié)果如下: 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)可以實現(xiàn)結(jié)果8 23 55,可惜不能實現(xiàn)判定字符串的前幾個字符是不是數(shù)字? 所以不知完全按你的思路,如何把82355 分開,后面要對這些數(shù)字進行處理,故而必須要將82355 分開~ |
木蟲 (正式寫手)
|
#稍微動下腦筋就可以解決的啊,代碼如下: 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() |

| 最具人氣熱帖推薦 [查看全部] | 作者 | 回/看 | 最后發(fā)表 | |
|---|---|---|---|---|
|
[考研] 070305高分子化學與物理 304分求調(diào)劑 +7 | c297914 2026-03-28 | 7/350 |
|
|---|---|---|---|---|
|
[考研] 0703 化學 求調(diào)劑,一志愿山東大學 342 分 +4 | Shern—- 2026-03-28 | 4/200 |
|
|
[考研] 一志愿鄭州大學,080500學碩,總分317分求調(diào)劑 +5 | 舉個栗子oi 2026-03-24 | 6/300 |
|
|
[考研] 316求調(diào)劑 +7 | 江辭666 2026-03-26 | 7/350 |
|
|
[考研] 295材料工程專碩求調(diào)劑 +7 | 1428151015 2026-03-27 | 7/350 |
|
|
[考研] 275求調(diào)劑 +10 | Micky11223 2026-03-25 | 14/700 |
|
|
[考研] 312,生物學求調(diào)劑 +3 | 小譯同學abc 2026-03-28 | 3/150 |
|
|
[考研] 304求調(diào)劑 +6 | 曼殊2266 2026-03-27 | 6/300 |
|
|
[考研] 藥學105500求調(diào)劑 +3 | Ssun。。 2026-03-28 | 3/150 |
|
|
[考研] 一志愿南京航空航天大學材料學碩求調(diào)劑 +3 | @taotao 2026-03-28 | 3/150 |
|
|
[考研] 308求調(diào)劑 +7 | 墨墨漠 2026-03-27 | 7/350 |
|
|
[考研]
|
18419759900 2026-03-25 | 8/400 |
|
|
[考研] 085600,材料與化工321分調(diào)劑 +4 | 大饞小子 2026-03-27 | 6/300 |
|
|
[考研] 求調(diào)劑,一志愿 南京航空航天大學大學 ,080500材料科學與工程學碩 +4 | @taotao 2026-03-26 | 5/250 |
|
|
[考研] 336材料求調(diào)劑 +7 | 陳瀅瑩 2026-03-26 | 9/450 |
|
|
[考研] 考研一志愿蘇州大學初始315(英一)求調(diào)劑 +3 | sbdksD 2026-03-24 | 4/200 |
|
|
[考研] 0854人工智能方向招收調(diào)劑 +4 | 章小魚567 2026-03-24 | 4/200 |
|
|
[考研] 086003食品工程求調(diào)劑 +6 | 淼淼111 2026-03-24 | 6/300 |
|
|
[考研] 318求調(diào)劑 +3 | plum李子 2026-03-23 | 3/150 |
|
|
[考研] 材料/農(nóng)業(yè)專業(yè),07/08開頭均可,過線就行 +3 | 呵唔哦豁 2026-03-23 | 4/200 |
|