3

I have a Latex Table that for weekly lesson plans, I want to pull data from syllabi that I will store as lists.(They will be read in from csv) So the syllabus is a list of 50 chapters and the latex has spaces for 2 lessons a week for 4th grade and 3 for sixth, I want to chomp the first lesson and stick in in the first token, then the next . . . right now my code would just give me chapter1 on Monday, Wed and Fri instead of 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}

here is the python

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)

which gives me

#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
  • 1
    What is your exact question? It would be much easier to answer it if you stated it clearly. Also, you should try to separate your concrete problem from the domain you're working in as much as possible - we don't want to study your big picture (schedule creation) just to answer you on some small issue (e.g. string splitting). Commented Nov 12, 2012 at 14:19

1 Answer 1

4

The problem with using

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

is that every occurrence of _thing_ is getting replaced with the same value, temp.

What we desire for here is a temp value that can change with each match.

re.sub provides such a facility through the use of a callback function as the second argument, rather than a string like temp.

The callback is simply a function that takes one argument, the match object, and returns the string we desire for that match.

def replacer(match):
    return ...

Now what to put in place of the ellipsis? We can use an iter here:

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'

So what we really want is a callback that looks like this:

def replacer(match):
    return next(data)

But we have more than one set of data: math6 and math4, for example. So we need a callback factory: a function that returns a callback given data:

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

Putting it all together,

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)    

yields

\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}
Sign up to request clarification or add additional context in comments.

3 Comments

I'm going to have to read up on iter I only know for while if elif so far, thanks so much for your help
what does the In [27]: mean? Is that from Komodo or IDLE like line #s?
Found it, looks like it's from ipython (kind of like irb).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.