Explore the latest version of An Elementary Introduction to the Wolfram Language »
44Importing and Exporting
Import a text version of the front page of the UN website (it might be different now):
In[1]:=
Click for copyable input
Out[1]=
The result is a string, possibly with some blank lines. Lets start by splitting the string at newlines.
In[2]:=
Click for copyable input
Out[2]=
In[3]:=
Click for copyable input
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]:=
Click for copyable input
Out[4]=
In[5]:=
Click for copyable input
Out[5]=
As a more sophisticated example, heres a graph of the hyperlinks in part of my website. To keep it manageable, Ive taken just the first 5 hyperlinks at each level, and gone only 3 levels.
In[6]:=
Click for copyable input
Out[6]=
In[7]:=
Click for copyable input
Out[7]=
In[8]:=
Click for copyable input
Out[8]=
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 webpages and files, but also with services or APIs. For example, SocialMediaData lets you get data from social media servicesat least once youve authorized them to send the data.
Find the network of my Facebook friends who give access to their connection data:
In[9]:=
Click for copyable input
Out[9]=
In[10]:=
Click for copyable input
Out[10]=
Request image thumbnails:
In[11]:=
Click for copyable input
Out[11]=
Theyre recognized as different kinds of birds:
In[12]:=
Click for copyable input
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 its all been set up to be easy to work with in the Wolfram Language.
You can find out whats 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.
In[13]:=
Click for copyable input
Out[13]=
In[14]:=
Click for copyable input
In[15]:=
Click for copyable input
In[16]:=
Click for copyable input
Out[16]=
In[17]:=
Click for copyable input
Out[17]=
Heres part of the resulting file:
Import the contents of the file back into the Wolfram Language:
In[18]:=
Click for copyable input
Out[18]=
The Wolfram Language can import and export hundreds of formats, of many different kinds.
In[19]:=
Click for copyable input
Out[19]=
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]:=
Click for copyable input
Out[20]=
In[21]:=
Click for copyable input
Out[21]=
44.1Import the images from http://google.com»
Sample expected output:
Out[]=
44.2Make an image collage of disks with the dominant colors from images on http://google.com»
Sample expected output:
Out[]=
Sample expected output:
Out[]=
44.4Make an image collage of the images on http://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 http://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! Youll get whatever they have on them now.
Why doesnt Import retrieve elements that I see when I visit a webpage in a browser?
Probably because theyre not directly present in the HTML source of the webpage, which is what Import looks at. Theyre probably being dynamically added using JavaScript.
What formats can Import handle?
How do Import and Export figure out what format to use?
Where does Export put the files it creates?
If you dont give a directory in the file name you specify, itll go in your current directory. You can open it as your operating system would using SystemOpen, and you can delete it with DeleteFile.
What is an API?
How do I authorize a connection to an external account of mine?
When you use SocialMediaData or ServiceConnect, youll typically be prompted to authorize the Wolfram Connection app for that particular service.
 
Download Notebook Version
es