WOLFRAM

44 Importing and Exporting

44Importing and Exporting
Everything we’ve done so far in this book has been done entirely within the Wolfram Language and the Wolfram Knowledgebase. But sometimes you need to get things from the outside. Needless to say, they often won’t be as clean and organized as what you’re used to inside the Wolfram Languageand they may change without warning.
Import a text version of the front page of the UN website (it might well be different now):
In[1]:=
Out[1]=
The result is a string, possibly with some blank lines. Let’s start by splitting the string at newlines.
Split at newlines to get a list of strings:
In[2]:=
Out[2]=
Identify the language for each string:
In[3]:=
Out[3]=
Import lets you import a wide variety of different elements. "Hyperlinks" gets hyperlinks that appear on a webpage; "Images" gets images.
Get a list of the hyperlinks on the front of the UN website:
In[4]:=
Out[4]=
In[5]:=
Out[5]=
As a more sophisticated example, here’s a graph of the hyperlinks in part of my website. To keep it manageable, I’ve taken just the first 5 hyperlinks at each level, and gone only 3 levels.
Compute part of the hyperlink graph for my website:
In[6]:=
Out[6]=
If you want to get an image of what a webpage looks like in a web browser window, you can do that with WebImage.
Get an image of the top of the current www.wolfram.com page:
In[7]:=
Out[7]=
You can do operations on this image, like replacing a color:
In[8]:=
Out[8]=
Import a picture from my website:
In[9]:=
Out[9]=
The Wolfram Language recognizes me!
In[10]:=
Out[10]=
As well as importing from the web, Import can also import from your own files, stored in your computer system or in the Wolfram Cloud.
The Wolfram Language lets you not only deal with specific webpages and files, but also lets you do things like search the web.
Search for images on the web using the keywords “colorful birds”:
In[11]:=
Out[11]=
They’re recognized as different kinds of birds:
In[12]:=
Out[12]=
An important source of external data for the Wolfram Language is the Wolfram Data Repository. The data in this repository comes from many placesbut it’s all been set up to be easy to work with in the Wolfram Language.
You can find out what’s available by browsing the Wolfram Data Repository.
Once you’ve found something you want, just use ResourceData["name"] to get it into the Wolfram Language.
Get the full text of Darwin’s On the Origin of Species, then make a word cloud of it:
In[13]:=
Out[13]=
Send oneself a message by email (for me it’s sending to me):
In[14]:=
Out[{}]=
Send email to a test account with subject “Wolf” and a picture of a wolf attached:
In[15]:=
If you want to interact with external programs and services, you’ll often have to export things from the Wolfram Language.
Export a graphic of a circle to the cloud in PDF format:
In[16]:=
Out[16]=
You can also export to local files using Export.
Export a table of primes and their powers to a local spreadsheet file:
In[17]:=
Out[17]=
Here’s part of the resulting file:
In[18]:=
Out[18]=
The Wolfram Language can import and export hundreds of formats, of many different kinds.
Export 3D geometry in a format suitable for 3D printing:
In[19]:=
Out[19]=
Here’s the result of 3D printing from the spikey.stl file:
Creating 3D geometry in a form suitable for printing can be quite complicated. The function Printout3D does all the steps automaticallyand it can also send the final geometry to a 3D printing service (or to your own 3D printer, if you have one).
Make a random clump of spheres:
In[20]:=
Out[20]=
In[21]:=
Out[21]=
Out[{}]=
44.1Import the images from https://www.google.com»
Sample expected output:
Out[]=
44.2Make a list of disks on gray backgrounds with the dominant colors from images on https://www.google.com»
Sample expected output:
Out[]=
44.3Make a word cloud of the text on https://bbc.co.uk»
Sample expected output:
Out[]=
44.4Make an image collage of the images on https://nps.gov»
Sample expected output:
Out[]=
44.5Use ImageInstanceQ to find pictures on https://en.wikipedia.org/wiki/Ostrich that are of birds. »
Sample expected output:
Out[]=
44.6Use TextCases with "Country" to find instances of country names on https://www.nato.int, and make a word cloud of them. »
Sample expected output:
Out[]=
44.7Find the number of links on https://en.wikipedia.org»
Sample expected output:
Out[]=
44.8Send yourself mail with a map of your current location. »
Sample expected output:
Out[]=
44.9Send yourself mail with an icon of the current phase of the moon. »
Sample expected output:
Out[]=
Why do I get different results when I run the website examples?
Because the websites have changed! You’ll get whatever they have on them now.
Why doesn’t Import retrieve elements that I see when I visit a webpage in a browser?
Probably because they’re not directly present in the HTML source of the webpage, which is what Import looks at. They’re probably being dynamically added using JavaScript. WebImage can give you an image of what a website looks like in a browser window.
Can I access password-protected websites using Import?
If you have a password, then yes, using the Authentication option of Import. There’s also a lower-level function URLDownload that lets you directly retrieve material from websites.
Can I import a local file from my computer if I’m using the cloud?
Yes. Use the upload  button in the cloud system to upload the file into your cloud file system, then use Import.
What formats can Import handle?
See the list at wolfr.am/ref-importexport or evaluate $ImportFormats.
How do Import and Export figure out what format to use?
You can explicitly tell them. Or they can determine it from file extensions, like .gif or .mbox.
Where does Export put the files it creates?
  • ImportString lets you “import” from a string rather than from an external file or URL. ExportString “exports” to a string.
  • SendMail uses either mail server preferences you set up, or a proxy in the Wolfram Cloud.
  • The Wolfram Language supports many external services through its ServiceConnect framework.
  • Another way to get (and send) data is through direct connection from your computer to a sensor, Arduino, etc. The Wolfram Language has a whole framework for dealing with such things, including functions such as DeviceReadTimeSeries.
  • If you’re running everything locally on your computer, you can have the Wolfram Language start external programs, and exchange data with them, for example using RunProcess. In simple cases, you’ll often be able to just pipe data straight in from a program, say with Import["!program", ...].
  • The Wolfram Language can access web APIs, supporting standard mechanisms for doing authentication. You can also set up your own web APIs in the Wolfram Language using APIFunction.
  • The Wolfram Language supports asynchronous reading and writing of data. A simple case is URLSubmit, but ChannelListen, etc. allow you to set up a complete brokered publish-subscribe system.
  • The Wolfram Language has extensive capabilities for dealing with blockchains. BlockchainBlockData retrieves current data on blocks.
Next Section