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 9269889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:14:06+00:00 2026-06-18T15:14:06+00:00

I am trying to write a script in Python 2.7.3 that can take a

  • 0

I am trying to write a script in Python 2.7.3 that can take a .csv file from an Excel spreadsheet and convert it to a format suitable for a LaTeX table. So I want to read a file, and write the data to a new text file, but with any commas replaced with ampersands, and a double backslash appended to the end of each line.

Example:
Input

A1,A2,A3  
B1,B2,B3  
C1,C2,C3

Desired Output

A1 & A2 & A3 \\
B1 & B2 & B3 \\
C1 & C2 & C3 \\

Here’s what I have right now:

old_file = open(selected_file, "r")
new_file = open("texified_" + selected_file.replace("csv","txt"), "w")
#Creates new file with format texified_selected_file.txt

for line in old_file:
    new_file.write(line.replace(",", " & ") + r" \\")

new_file.close()
old_file.close()

Right now it properly replaces the commas with the ampersand but doesn’t add the double backslash. I thought this was because the backslash has special meaning, but even when making it a raw string it still doesn’t work. It does add it to the end of the final line, however.

Actual Output

A1 & A2 & A3   
B1 & B2 & B3  
C1 & C2 & C3 \\
  • 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-18T15:14:08+00:00Added an answer on June 18, 2026 at 3:14 pm

    I’m not sure whats wrong with your code (or with your input data), but I’d probably do it similar to this (probably less verbose):

    for line in old_file:
        line = line.strip()     # remove newline/whitespace from begin and end of line
        line = line.split(',')  # get comma-separated values
        line = " & ".join(line) # make it ampersand-separated values
        line += r" \\"          # add latex line break
        line += "\n"            # add file line break
        new_file.write(line)
    

    Or this way:

    import jinja2
    
    # define the latex template
    template_str = r"""
    \documentclass{article}
    \begin{document}
    \begin{table}
      \centering
      \begin{tabular}{ccc}
    %{ for line in table %} %{{line[0]%}} & %{{line[1]%}} & %{{line[2]%}} \\ 
    %{ endfor %}
      \end{tabular}
    \end{table}
    \end{document}
    
    """
    
    # initialize the rendering engine
    renderer = jinja2.Environment(
      block_start_string = '%{',
      block_end_string = '%}',
      variable_start_string = '%{{',
      variable_end_string = '%}}'
    )
    template = renderer.from_string(template_str)
    
    # bring the data array into shape
    lines = [line.strip().split(',') for line in old_file]
    
    # generate the tex source code
    with open("test.tex", 'w+') as f:
      f.write(template.render(table=lines))
    

    Also have a look at these resources:

    • Python csv-module for reading csv files
    • Jinja2 for template rendering
    • https://tex.stackexchange.com/a/40728/10704 for a great example
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a small Python script that will get query results from
I am trying to write a Python script that can move and copy files
I want to write a Python script that reads in an Excel spreadsheet and
I'm trying to write a Python script that uses a particular external application belonging
I am trying to write a python script that takes record data like this
I am trying to write a python script that will change my cwd to
I'm trying to write a script that can read several .xml files within a
I'm trying to write a script in Python that reloads a page every x
I am trying to write a python script that I could easily export to
I'm trying to write a simple Python script that inserts .odt documents into an

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.