44 | Importing 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 Language—and they may change without warning.
As a first example, let’s import the text from the front page of the United Nations website. We can do this using the function Import.
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]=
The Wolfram Language can import hundreds of formats—including spreadsheets, images, audio, video, geometry, databases, log files, PDFs and more. Import will automatically look at the file extension (.png, .xls, etc.) to determine what to do.
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 places—but 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]=
In addition to getting things into the Wolfram Language, you can also send things out. For example, SendMail sends email from the Wolfram Language.
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 automatically—and 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[{}]=
Import[loc] | import from an external location | |
WebImageSearch["keyword"] | get images by searching the web | |
ResourceData["name"] | get data from the Wolfram Data Repository | |
SendMail[expr] | send email | |
CloudExport[expr,format] | export in a certain format to the cloud | |
Export[file,expr] | export to a file | |
Printout3D[source,"service"] | send source to a 3D printer service |
44.2Make a list of disks on gray backgrounds with the dominant colors from images on https://www.google.com. »
44.5Use ImageInstanceQ to find pictures on https://en.wikipedia.org/wiki/Ostrich that are of birds. »
44.6Use TextCases with "Country" to find instances of country names on https://www.nato.int, and make a word cloud of them. »
44.8Send yourself mail with a map of your current location. »
44.9Send yourself mail with an icon of the current phase of the moon. »
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.
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?
If you don’t give a directory in the file name you specify, it’ll go in your current directory. You can open it as your operating system would using SystemOpen, and you can delete it with DeleteFile.
- 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.