Shakespeare: Difference between revisions
Content added Content deleted
imported>Zanwenhuahao |
imported>Zanwenhuahao |
||
Line 40: | Line 40: | ||
True |
True |
||
</pre> |
</pre> |
||
====File Operations==== |
|||
* Open a file |
|||
<pre> |
|||
>>> M_S=open("A Midsummer-Night's Dream.txt", "r") |
|||
>>> M_S |
|||
<open file "A Midsummer-Night's Dream.txt", mode 'r' at 0x102d04660> |
|||
</pre> |
|||
* Read a line |
|||
<pre> |
|||
>>> M_S = open("A Midsummer-Night's Dream.txt", "r") |
|||
>>> line = M_S.readline() |
|||
>>> print line |
|||
< Shakespeare -- A MIDSUMMER-NIGHT'S DREAM > |
|||
</pre> |
|||
* Read a file line by line until the end of file (also known as <cod> eof </code>) |
|||
<pre> |
|||
>>> for eachline in M_S: |
|||
>>> # Do something here with each line read |
|||
... |
|||
</pre> |
|||
====Strings Exercise==== |
|||
* Using the play <b> Romeo and Juliet </b>, |
Revision as of 07:32, 27 September 2014
How many times does the word 'love' appear in Shakespeare's plays? Is it possible to find negative passages using a list of keywords? We'll use Python to practice our skills and answer questions like these.
Setup
We'll need additional setup here.
Goals
- Have fun using Python to learn basic data science.
- Practice searching for information in text documents
- Practice manipulating strings
- Practice using loops
- Practice using lists
- Practice using dictionaries.
- Get experience with regular expressions.
Skills & Exercises
Strings
- Checking if two strings are equal
>>> s = "mama" >>> s == "mama" True >>> s == "papa" False
- Checking if a string contains another as a substring
>>> s = "mama" >>> s in "I love mama" True >>> "day" in "Saturday" True >>> "Day" in "Saturday" False >>> Sat = "Saturday" >>> "day" in Sat True
File Operations
- Open a file
>>> M_S=open("A Midsummer-Night's Dream.txt", "r") >>> M_S <open file "A Midsummer-Night's Dream.txt", mode 'r' at 0x102d04660>
- Read a line
>>> M_S = open("A Midsummer-Night's Dream.txt", "r") >>> line = M_S.readline() >>> print line < Shakespeare -- A MIDSUMMER-NIGHT'S DREAM >
- Read a file line by line until the end of file (also known as <cod> eof )
>>> for eachline in M_S: >>> # Do something here with each line read ...
Strings Exercise
- Using the play Romeo and Juliet ,