파이썬 초보자를 도울 수 있다면 정말 좋겠다. 공백이있는 모든 문자열 사이파이썬에서 텍스트를 분석하고 정규식으로 추출하기
000001 A040C015_130223_R1WV V C 11:37:48:22 11:38:29:18 10:00:53:00 10:01:33:20
:
나는 다음과 같은 형식의이 같은 라인의 많은 양을 가지고있는 textdocument를 분석하고자합니다. 그래서 나는 다음과 같은 한 : 내가 가지고있는
#writing data into list
datalist = []
filedata = open(inputfile, 'r')
for line in filedata:
line = line.strip('\n\t\r')
datalist.append(line)
filedata.close()
#splitting up List by whitespace and creating new List
newList = []
for i in datalist:
newList.append(i.split(' '))
print newList[0:]
#parsing list with regex
regCompiled = re.compile('^[A-Z][0-9]{3,3}[C][0-9]{3,3}[_][0-9]{6,6}[_][A-Z][0-9]{2,2}[A-Z].*');
for content in newList:
checkMatch = re.match(regCompiled, content);
if checkMatch:
print ("Found:"), content
else:
print ("NO Match")
첫 번째 문제는 모든 라인 분할 후 모든 공백에 대한 ('') 빈 항목 와 목록을 만드는 것 같다이며, 그것은처럼 그것은 보인다 분할 기능 때문에 목록에있는 목록.
제가filter(None, newList)
으로 시도하지만 ('')은 나머지 항목 때문에 빈 항목 정규식 오류된다. 결국 A040C015_etc가 포함 된 문자열 만 추출합니다.
전체 textlist
은 여기에 있습니다 : Link to full Text Document어떤 도움 주셔서 대단히 감사합니다 ... 라이너
그 정규식은 정확히 무엇입니까? – WeaselFox
이 문자열 형식을 정확하게 찾아야합니다. A040C015_130223_R1WV – rainer