2012-11-12 2 views
3

나는 매주 강의 계획을 위해 목록으로 저장할 강의 테이블에서 자료를 가져오고 싶다. (그들은 csv에서 읽힐 것이다) 그래서 강의 계획서는 50 개의 목록이다. 챕터와 라텍스는 4 학년 때 1 주일에 2 레슨, 6 학년 때 3 레슨의 공간이 있습니다. 첫 레슨을 시작하고 첫 번째 토큰을 고수하고 다음 토큰을 붙이고 싶습니다. . . 지금 내 코드는regex 목록에서 바꾸기

여기
math6 = ['chapter1', 'chapter2', 'chapter3', 'chapter1-3test'] 
math4= ['chapter1.1', 'chapter1.2-3', 'chapter2'] 

    \begin{tabular}{|p{0.7in}|p{0.8in}|p{2.2in}|p{.9in}|p{2.6in}|p{1.6in}|} 
    6${}^{th}$ Math \newline M\newline & _math6_& 
    6${}^{th}$ Math \newline W \newline & _math6_ & 
    6${}^{th}$ Math \newline F \newline & _math6_ & 
    4${}^{th}$ Math \newline M\newline & & _math4_ & 
    4${}^{th}$ Math \newline W\newline & & _math4_ & 
    \end{tabular} 

나에게

#contents of file1 
# Really long Latex 
#File that has 
# geography, geography, mary, tom, susan, geography 
#that I want to replace 
#read file1 in as a string, replace, save again 

#contents of file1 
# Really long Latex 
#File that has 
# physics, physics, mary, tom, susan, physics 
#that I want to replace 
#read file1 in as a string, replace, save again 

#contents of file1 
# Really long Latex 
#File that has 
# hairdressing, hairdressing, mary, tom, susan, hairdressing 
#that I want to replace 
#read file1 in as a string, replace, save again 

#contents of file1 
# Really long Latex 
#File that has 
# torah, torah, mary, tom, susan, torah 
#that I want to replace 
#read file1 in as a string, replace, save again 
+1

정확한 질문은 무엇입니까 그래서 우리는 콜백 공장이 필요 ? 당신이 그것을 분명하게 말하면 대답하기가 훨씬 쉬울 것입니다. 또한 가능한 한 많이 작업하고있는 도메인과 구체적 문제를 분리시켜야합니다. 작은 문제 (예 : 문자열 분할)에 대해서만 답변하기 위해 큰 그림 (일정 만들기)을 연구하고 싶지는 않습니다. . – metakermit

답변

3
을 제공 파이썬

import re 
template = file('file1.txt', 'r').read() 

lost= ["geography", "physics", "hairdressing", "torah"] 
n =0 
while n<len(lost): 
    temp=lost[n] 
    page= re.sub(r'_thing_', temp, template) 
    print page 
    n+=1 
#page= re.sub(r'_thing_', "martha", template) 
#file('result.txt', 'w').write(page) 

입니다 CH3, 나 월요일, 수요일과 금 대신에 CH1, CH2의에 장 줄 것

사용상의 문제점

re.sub(r'_thing_', temp, template) 

입니다. 모든 발생이 _thing_ 인 경우 동일한 값인 temp으로 바뀝니다.

여기서는 각 일치 항목마다 변경할 수있는 temp 값을 지정합니다.

re.sub provides such a facilitytemp과 같은 문자열이 아니라 두 번째 인수로 콜백 함수를 사용합니다.

콜백은 단순히 하나의 인수 인 match 객체를 사용하고 해당 일치에 대해 원하는 문자열을 반환하는 함수입니다.

def replacer(match): 
    return ... 

이제 생략 기호 대신 무엇을 넣을 까? 우리는 iter 여기에 사용할 수 있습니다

In [27]: math6 = ['chapter1', 'chapter2', 'chapter3', 'chapter1-3test'] 

In [28]: math6 = iter(math6) 

In [29]: next(math6) 
Out[29]: 'chapter1' 

In [30]: next(math6) 
Out[30]: 'chapter2' 

그래서 우리가 정말 원하는 것은 다음과 같습니다 콜백입니다 :

def replacer(match): 
    return next(data) 

그러나 우리는 하나 이상의 데이터 설정 : 예를 들어, math6math4을 . data 주어진 콜백 반환하는 함수 :

def replace_with(data): 
    def replacer(match): 
     return next(data) 
    return replacer 

이 모두 함께 퍼팅,

import re 

math6 = iter(['chapter1', 'chapter2', 'chapter3', 'chapter1-3test']) 
math4 = iter(['chapter1.1', 'chapter1.2-3', 'chapter2']) 

text = r''' 
    \begin{tabular}{|p{0.7in}|p{0.8in}|p{2.2in}|p{.9in}|p{2.6in}|p{1.6in}|} 
    6${}^{th}$ Math \newline M\newline & _math6_& 
    6${}^{th}$ Math \newline W \newline & _math6_ & 
    6${}^{th}$ Math \newline F \newline & _math6_ & 
    4${}^{th}$ Math \newline M\newline & & _math4_ & 
    4${}^{th}$ Math \newline W\newline & & _math4_ & 
    \end{tabular} 
''' 

def replace_with(data): 
    def replacer(match): 
     return next(data) 
    return replacer 

for pat, data in [(r'_math6_', math6), (r'_math4_', math4)]: 
    text = re.sub(pat, replace_with(data), text) 

print(text)  

수익률

\begin{tabular}{|p{0.7in}|p{0.8in}|p{2.2in}|p{.9in}|p{2.6in}|p{1.6in}|} 
6${}^{th}$ Math \newline M\newline & chapter1& 
6${}^{th}$ Math \newline W \newline & chapter2 & 
6${}^{th}$ Math \newline F \newline & chapter3 & 
4${}^{th}$ Math \newline M\newline & & chapter1.1 & 
4${}^{th}$ Math \newline W\newline & & chapter1.2-3 & 
\end{tabular} 
+0

나는 지금까지 엘프가 있다면, 당신의 도움에 대해 많은 감사를 드리며 iter를 읽어야 할 것입니다. –

+0

In [27] :는 무엇을 의미합니까? 코모도 (Komodo) 나 유령 (IDLE)과 같은 라인인가요? –

+0

찾았습니다. ipython (irb와 같은 종류)에서 나온 것 같습니다. –