3

毎週の授業計画のために、リストとして保存するシラバスからデータを取得したいラテックス テーブルがあります。 4 年生は週に 3 回、6 年生は週に 3 回、最初のレッスンをむさぼり食って、最初のトークンに固執し、次の . . . 今、私のコードは、ch1、ch2、ch3 ではなく、月曜、水曜、金曜にチャプター 1 を与えるだけです。

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}

ここにパイソンがあります

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)

それは私に与えます

#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
4

1 に答える 1

3

使用上の問題

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

が出現するたび_thing_に、同じ値 に置き換えられるということですtemp

ここで求めているのは、temp試合ごとに変化する値です。

re.subのような文字列ではなく、コールバック関数を 2 番目の引数として使用することで、このような機能を提供しtempます。

コールバックは、1 つの引数 (一致オブジェクト) を取り、その一致に必要な文字列を返す単純な関数です。

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)

しかし、複数のデータ セットがあります。たとえば、math6andです。math4したがって、コールバック ファクトリが必要です。指定されたコールバックを返す関数です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}
于 2012-11-12T14:25:36.903 に答える