2 回答

TA貢獻1825條經(jīng)驗 獲得超6個贊
我相信要解決您當前的問題,如果您只嘗試解析一行,您只需將第二line = fp.readline()行移動到 while 循環(huán)的末尾。目前,您實際上是從第二行開始解析,因為您已經(jīng)readline在示例代碼的第一行中使用了 a 。
更改后將如下所示:
line = fp.readline() # read in the first line
fw = open('output.txt', "w+")
cnt = 1
while line:
print("Line {}: {}".format(cnt, line.strip()))
line = line.split(')')[0]
fw.write(line + "\n")
cnt += 1
line = fp.readline() # read in next line after parsing done
您的示例輸入文本的輸出:
WELD 190 制造 I MasterCAM 簡介(3

TA貢獻1860條經(jīng)驗 獲得超8個贊
假設您的其他類文本塊與您顯示的具有相同的結構,您可能希望使用正則表達式來提取類名和類號:
下面我假設每個文本塊都包含信息“XX 小時講座”,順序相同,其中“XX”代表任何類型的數(shù)字(時間范圍)。在變量“match_re”中,我定義了一個正則匹配表達式以僅匹配定義的點“XX 小時講座”。通過使用“match.group(2)”,我將匹配限制在最里面的括號對中的部分。
下面的匹配表達式可能對您來說還不完整,因為我不知道您的整個文本文件。
下面我提取字符串:WELD 190 Manufacturing I MasterCAM簡介(3)
import re
string = "WELD 190 Manufacturing I Introduction to MasterCAM (3) 1? hours lecture - 4? hours laboratory Note: Cross listed as DT 190/ENGR 190/IT 190 This course will introduce the students to MasterCAM and 2D and basic 3D modeling. Students will receive instructions and drawings of parts requiring 2- or 3-axis machining. Students will design, model, program, set-up and run their parts on various machines, including plasma cutters, water jet cutters and milling machines. WELD 197 Welding Technology Topics (.5 - 3)"
match_re = "(^(.*)\d.* hours lecture)"
match = re.search(match_re,string)
if match:
print(match.group(2))
else:
print("No match")
添加回答
舉報