3

我有一个乳胶表,用于每周的课程计划,我想从教学大纲中提取数据,并将其存储为列表。(它们将从 csv 中读取)所以教学大纲是 50 个章节的列表,乳胶有 2 个空间四年级每周一节课,六年级每周三节课,我想咬第一节课,坚持第一节课,然后下一节课。. . 现在我的代码只会在星期一、星期三和星期五给我第 1 章,而不是 ch1、ch2、ch3

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通过使用回调函数作为第二个参数而不是像temp.

回调只是一个函数,它接受一个参数,即匹配对象,并返回我们想要匹配的字符串。

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}
于 2012-11-12T14:25:36.903 回答