[q-lang-cvs] q-csv/examples readsamples.q, 1.4, 1.5 writesamples.q, 1.4, 1.5
Brought to you by:
agraef
From: RER <ed...@us...> - 2008-01-22 16:45:39
|
Update of /cvsroot/q-lang/q-csv/examples In directory sc8-pr-cvs16.sourceforge.net:/tmp/cvs-serv8598/examples Modified Files: readsamples.q writesamples.q Log Message: Changed module to be loosely compatible with Python's CSV module Index: writesamples.q =================================================================== RCS file: /cvsroot/q-lang/q-csv/examples/writesamples.q,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** writesamples.q 18 Jan 2008 21:34:13 -0000 1.4 --- writesamples.q 22 Jan 2008 16:45:17 -0000 1.5 *************** *** 15,36 **** /* Illustration of fwritecsv ! Note that certain fields are not quoted. */ writefile F [] = (); ! writefile F [L|Ls] = [fwritecsv_data F L | writefile F Ls]; /* Illustration of writecsv_string Note that all fields are quoted. */ writefile_string F [] = (); ! writefile_string F [L|Ls] = [fwritecsv_string F L | writefile_string F Ls]; /* Illustration of fwritecsv with tab delimeter */ writefile_tab F [] = (); ! writefile_tab F [L|Ls] = [fwritecsv_data (F, "\t", "\"") L | writefile_tab F Ls]; /* Illustration of fwritecsv with tab and quote delimeters */ writefile_tab_quote F [] = (); ! writefile_tab_quote F [L|Ls] = [fwritecsv_data (F, "\t", "'") L | writefile_tab_quote F Ls]; def Sample1 = [("this", " that ", 23, -3.0, ""), ("a \"b\"", "c c", 10, 3.2, " "), --- 15,46 ---- /* Illustration of fwritecsv ! Note that interger and float fields are not quoted. */ writefile F [] = (); ! writefile F [L|Ls] = [fwritecsv F L | writefile F Ls]; /* Illustration of writecsv_string Note that all fields are quoted. */ + def Dialect1 = csv_dialect [csv_quoting,csv_quote_all;]; writefile_string F [] = (); ! writefile_string F [L|Ls] = [fwritecsv (F,Dialect1) L ! | writefile_string F Ls]; /* Illustration of fwritecsv with tab delimeter */ + def Dialect2 = csv_dialect [csv_delimiter,"\t";]; writefile_tab F [] = (); ! writefile_tab F [L|Ls] = [fwritecsv (F, Dialect2) L | writefile_tab F Ls]; /* Illustration of fwritecsv with tab and quote delimeters */ + def Dialect3 = csv_dialect [csv_delimiter,"\t"; csv_quote,"'";]; writefile_tab_quote F [] = (); ! writefile_tab_quote F [L|Ls] = [fwritecsv (F, Dialect3) L | writefile_tab_quote F Ls]; + /* Illustration of fwritecsv with escape char */ + def Dialect4 = csv_dialect [csv_escape,"%";]; + writefile_escape F [] = (); + writefile_escape F [L|Ls] = [fwritecsv (F, Dialect4) L + | writefile_escape F Ls]; + def Sample1 = [("this", " that ", 23, -3.0, ""), ("a \"b\"", "c c", 10, 3.2, " "), *************** *** 56,58 **** || writes "Writing 'write-sample4.csv:' (tab delimited, single quoted)" || writefile_tab_quote (fopen "write-sample4.csv" "w") Sample3 ! || writes "\ndone.\n\n-----"; \ No newline at end of file --- 66,71 ---- || writes "Writing 'write-sample4.csv:' (tab delimited, single quoted)" || writefile_tab_quote (fopen "write-sample4.csv" "w") Sample3 ! || writes "\ndone.\n\n-----" ! || writes "Writing 'write-sample5.csv:' (quotes are escaped)" ! || writefile_escape (fopen "write-sample5.csv" "w") Sample1 ! || writes "\ndone.\n"; \ No newline at end of file Index: readsamples.q =================================================================== RCS file: /cvsroot/q-lang/q-csv/examples/readsamples.q,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** readsamples.q 18 Jan 2008 21:34:13 -0000 1.4 --- readsamples.q 22 Jan 2008 16:45:16 -0000 1.5 *************** *** 17,34 **** Note that integer and float fields are automatically converted. */ readfile F = [] if feof F; ! = [freadcsv_data F | readfile F]; /* Illustration of freadcsv_string Note that integer and float fields are interpreted as strings. */ readfile_string F = [] if feof F; ! = [freadcsv_string F | readfile_string F]; /* Illustration of freadcsv with tab delimeter */ readfile_tab F = [] if feof F; ! = [freadcsv_data (F, "\t", "\"") | readfile_tab F]; /* Illustration of freadcsv with tab and quote delimeters */ readfile_tab_quote F = [] if feof F; ! = [freadcsv_data (F, "\t", "'") | readfile_tab_quote F]; main = writes "Reading 'read-sample1.csv:' (standard CSV)\n" --- 17,41 ---- Note that integer and float fields are automatically converted. */ readfile F = [] if feof F; ! = [freadcsv F | readfile F]; /* Illustration of freadcsv_string Note that integer and float fields are interpreted as strings. */ + def Dialect1 = csv_dialect [csv_quoting,csv_quote_all;]; readfile_string F = [] if feof F; ! = [freadcsv (F, Dialect1) | readfile_string F]; /* Illustration of freadcsv with tab delimeter */ + def Dialect2 = csv_dialect [csv_delimiter,"\t";]; readfile_tab F = [] if feof F; ! = [freadcsv (F, Dialect2) | readfile_tab F]; /* Illustration of freadcsv with tab and quote delimeters */ + def Dialect3 = csv_dialect [csv_delimiter,"\t"; csv_quote,"'";]; readfile_tab_quote F = [] if feof F; ! = [freadcsv (F, Dialect3) | readfile_tab_quote F]; ! ! def Dialect4 = csv_dialect [csv_escape,"%";]; ! readfile_escape F = [] if feof F; ! = [freadcsv (F, Dialect4) | readfile_escape F]; main = writes "Reading 'read-sample1.csv:' (standard CSV)\n" *************** *** 44,47 **** --- 51,57 ---- || write (readfile_tab_quote (fopen "read-sample3.csv" "r")) || writes "\n\n-----" + || writes "Reading 'write-sample5.csv' (escaped quotes)\n" + || write (readfile_escape (fopen "write-sample5.csv" "r")) + || writes "\n\n-----" || writes "Reading 'read-sample4.csv:' (Malformed)\n" || write (readfile (fopen "read-sample4.csv" "r")); \ No newline at end of file |