Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8819435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:21:30+00:00 2026-06-14T05:21:30+00:00

I have a Latex Table that for weekly lesson plans, I want to pull

  • 0

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 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T05:21:31+00:00Added an answer on June 14, 2026 at 5:21 am

    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}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a LaTeX table that looks like this: \begin{table}[!ht] \centering \small \caption{ \bf{Caption}}
I have a large table I want to include in a LaTeX document. It
I have a table that includes the following column: mytable <- data.frame(beta_0 = c(1,2,3)
I have a really long table in LaTeX that spans several pages and is
I have a table that has checkboxes, if a checkbox is selected i want
I want to log the keys in a table that i have designed in
I'm writing an application that will have a SQL Server backend that will store
I have a table in LaTeX which spans multiple pages. I want the top
I have a string that is a LaTeX table. I'm trying to find the
I have a data table that is periodically updated by another service. I log

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.