Functions of esProc/R/Python/Perl in Structured Data Process by Comparison :Chapter 17.Writing to TEXT

esProc

By utilizing the export function, data can be exported to txt file with one statement. It provides a wealth of options and parameters as below:

f.export(A,x:F,…;s)      Export x,… of A, the field is named with F,…; if no x, export all the fields. The text will be exported by default; s is a separator; /t indicates ignored

@t       Including the title, if no Fi, use the column name of first record

@a      Append to write, and ignore @t 

Perl

Perl is easy to write to the txt file, the sample code is given as below:

    #!perl

    @carts=([173744,”aaa”,14],           #2D array to be written to the file

                  [173745,”ccc”,18],

                  [173746,”bbb”,13],);

    open TXT,”>a.txt”;                              #If > is changed to >>, append to the original file

    foreach(@carts){                                 #join function concatenates the arrays into a string with the                                                               separators

    print TXT join(“\t”,@$_).”\n”;         

    }

    close TXT;

But compared with esProc which can solve it only with one statement, it is still much worse than esProc.

Python

    myfile = open(“outfile.txt”,’w’)        #‘w’ indicates to overwrite; ‘a’ indicates to append

    data=[[‘5′,’dave’,’138888888888′],

              [‘6′,’oracle’,’13888888888′],

              [‘7′,’anqing’,’13888888888′]]

for line in data:

  1. write(‘\t’.join(line)) #What annoys you here is that joint can only concatenate the strings. If this is an integer or other type of data, only when it has been converted into a string manually can you use Join to concatenate them
  2. write(“\n”)
  3. close() 

It shows as complex as Perl does, and is a bit more cumbersome than esProc.

R

write.table(tbl,file=”d:/originalData.txt”,append=FALSE,quote=FALSE,sep=”\t”,col.names=TRUE,row.names=FALSE)

As good as esProc’s accessibility, R has more options, for example, to specify string data will be written in the quotation marks by quote=TRUE, also specify how to process the quotation marks in the value, whether to add the escape character or other symbols and so on.

In addition to column separator, you can also specify the character of line separator, while it doesn’t have to use \n.

And also by using the two options, i.e. na and dec, you can separately specify the representations of missing field value and decimal points.

2014-08-12_110549

Advertisements

About datathinker

a technical consultant on Database performance optimization, Database storage expansion, Off-database computation. personal blog at: datakeywrod, website: raqsoft
This entry was posted in Structured Data Process. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s