help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Importing tab-delimited text files or connecting to ODBC


From: Markus Triska
Subject: Re: Importing tab-delimited text files or connecting to ODBC
Date: Fri, 17 Feb 2006 00:33:45 +0100
User-agent: Mozilla Thunderbird 1.0.7 (X11/20051010)

Hi!

Maks Romih wrote:

What would be the best way to import some tables, like a tab delimited
copy-paste chunk from excel. I would like to get the table into Emacs
Lisp so that a table would be a big list of rows, where a row would be
a list of atoms, either raw strings or, when possible, converted to
Lisp numbers and symbols.

You can use R (www.r-project.org) to handle a number of different table formats uniformly. For example, given test.txt like this:

# directory size unit
/home/tester1 200 MB
/home/tester2 3000 KB
/home/tester3 250 MB

you can read it as a data frame:

> data <- read.table("test.txt", as.is=c(T,F,T), skip=1)

You then use a function like this to print it the way you want:

elisp <- function (df) {
        cat("(")
        for (i in 1:(dim(df)[1])) {
                cat("\n(")
                for (j in 1:dim(df)[2]) {
                        val <- df[i,j]
                        if (is.numeric(val)) {
                                cat(" ", val, " ", sep="")
                        } else {
                                cat(" \"", val, "\" ", sep="")
                        }
                }
                cat(")\n")
        }
        cat(")\n")
}


> elisp(data)
(
( "/home/tester1"  200  "MB" )

( "/home/tester2"  3000  "KB" )

( "/home/tester3"  250  "MB" )
)

These steps can be automated using R's batch facilities that you can call from Emacs. Some of the other conversions you mention can probably also be handled more directly in the R code.

All the best,
Markus.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]