Benjamin Dubreu
Benjamin Dubreu

Reputation: 97

Open a Latex file with Pandas?

I am trying to replicate using Python the content of the "Tidy Data" paper available here.

However, the datasets are available on github as .tex files, and I can't seem to be able to open them with pandas.

To the extent of my searches so far, it seems that pandas can export to latex, but not import from it...

1) Am I correct ? 2) If so, how would you advise me to open those files ?

Thank you for your time !

Upvotes: 0

Views: 763

Answers (2)

Corentin Limier
Corentin Limier

Reputation: 5006

Using this as example :

import pandas as pd
from pandas.compat import StringIO

with open('test.tex') as input_file:
    text = ""
    for line in input_file:
        if '&' in line:
            text += line.replace('\\', '') + '\n'

data = StringIO(text)
df = pd.read_csv(data, sep="&")
data.close()

Returns :

    year    artist          track                   time    date.entered    wk1 wk2 wk3
0   2000    2 Pac           Baby Don't Cry          4:22    2000-02-26      87  82  72
1   2000    2Ge+her         The Hardest Part Of ... 3:15    2000-09-02      91  87  92
2   2000    3 Doors Down    Kryptonite              3:53    2000-04-08      81  70  68
3   2000    98verb|^|0      Give Me Just One Nig... 3:24    2000-08-19      51  39  34
4   2000    A*Teens         Dancing Queen           3:44    2000-07-08      97  97  96
5   2000    Aaliyah         I Don't Wanna           4:15    2000-01-29      84  62  51
6   2000    Aaliyah         Try Again               4:03    2000-03-18      59  53  38
7   2000    Adams, Yolanda  Open My Heart           5:30    2000-08-26      76  76  74

You can also write one script which transform the file :

with open('test.tex') as input_file:
    with open('test.csv', 'w') as output_file:
        for line in input_file:
            if '&' in line:
                output_file.write(line.replace('\\', '') + '\n')

Then another script wich uses pandas

import pandas as pd
pd.read_csv('test.csv', sep="&")

Upvotes: 3

Émile Bernard
Émile Bernard

Reputation: 56

1) To my knowledge you can open any standard type of file with python

2) You could try:

with open('test.tex', 'w') as text_file:
    //Do something to text_file here

Upvotes: -1

Related Questions