6
$\begingroup$

I am consuming a webservice through NETLink that returns data directly in the response as a gzipped csv file.

I can successfully process the data like this:

binz=response@data
BinaryWrite["tmp.csv.gz", binz]
Close["tmp.csv.gz"]
Import["tmp.csv.gz", "CSV"]

However, I am curious if there is a more direct way to achieve this? Thanks for any advice.

$\endgroup$
0
8
$\begingroup$

The following seems to work:

(* Simulate your response *)
binz = ExportString[ {"A System for Doing", "Mathematics by Computer"}, {"GZIP", "CSV"} ];

(*Uncompress and read CSV*)
ImportString[binz, {"GZIP", "CSV"}]
(*
{{"A System for Doing"}, {"Mathematics by Computer"}}

*)

You may prefer to post a small data sample to test with your actual data format.

$\endgroup$
4
$\begingroup$

Base64 binary to text encoding may help, ie:

turtle = Import["ExampleData/turtle.jpg"];
gif = ExportString[turtle, "GIF"];
encoded = ExportString[gif, "Base64"];
ImportString[encoded, "Base64"]

enter image description here

However, Mathematica 7 requires this method:

turtle = Import["ExampleData/turtle.jpg"];
encoded = ExportString[turtle, {"Base64", "GIF"}];
ImportString[encoded, "Base64"]

For sending through NETLink you can use this:

kernel.Compute("ExportString[Graphics[Rectangle[]],{\"Base64\",\"EMF\"}]");
byte[] decodedBytes = Convert.FromBase64String(kernel.Result.ToString());
File.WriteAllBytes("C:\\Temp\\output.emf", decodedBytes);
$\endgroup$
3
  • $\begingroup$ +1. Did not know the form ExportString[expr, {"format", "format", ...}]. Where is it documented? It is equivalent to Fold[ExportString, expr, Reverse@{"format", "format", ...}]. For example, try: ExportString[Graphics[Rectangle[]], {"UUE", "Base64", "UUE", "EMF"}] === Fold[ExportString, Graphics[Rectangle[]], Reverse@{"UUE", "Base64", "UUE", "EMF"}]. $\endgroup$ Jun 11 '12 at 8:48
  • $\begingroup$ As far as I know the ExportString/ImportString combo uses temporary files to work on. So, although you're not explicitely using files, you're still doing it under the hood. $\endgroup$ Jun 11 '12 at 9:05
  • $\begingroup$ @ Alexey - I believe I found this syntax on the MathGroup. It doesn't seem to be eqivalent to Fold at least on Mma 7, since this does not work: encoded = Fold[ExportString, turtle, {"GIF", "Base64"}]; $\endgroup$ Jun 11 '12 at 9:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.