【问题标题】:regex replace from a list从列表中替换正则表达式
【发布时间】:2012-11-12 14:13:17
【问题描述】:

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

【问题讨论】:

  • 您的具体问题是什么?如果你说清楚了,回答起来会容易得多。此外,您应该尽量将您的具体问题与您正在工作的领域分开 - 我们不想研究您的大局(计划创建)只是为了回答一些小问题(例如字符串拆分) .

标签: python regex file-io replace latex


【解决方案1】:

使用问题

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

_thing_每一个 出现都被替换为相同的值,temp

我们在这里需要的是一个temp 值,它可以随着每次匹配而改变。

re.sub provides such a facility 通过使用回调函数作为第二个参数,而不是像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}

【讨论】:

  • 我将不得不阅读 iter 我只知道 if elif 到目前为止,非常感谢您的帮助
  • In [27]: 是什么意思?是来自 Komodo 还是 IDLE 之类的 #s 行?
  • 找到了,好像来自 ipython(有点像 irb)。
猜你喜欢
  • 1970-01-01
  • 2019-01-24
  • 1970-01-01
  • 2015-01-24
  • 1970-01-01
  • 1970-01-01
  • 2023-01-11
  • 2011-07-13
相关资源
最近更新 更多