io.asciidoc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. [[io]]
  2. == Input and Output
  3. There will be situations where your program has to interact with the user. For example, you would
  4. want to take input from the user and then print some results back. We can achieve this using the
  5. `raw_input()` function and `print` statement respectively.
  6. For output, we can also use the various methods of the `str` (string) class. For example, you can
  7. use the `rjust` method to get a string which is right justified to a specified width. See
  8. `help(str)` for more details.
  9. Another common type of input/output is dealing with files. The ability to create, read and write
  10. files is essential to many programs and we will explore this aspect in this chapter.
  11. === Input from user
  12. Save this program as `io_input.py`:
  13. [source,python]
  14. --------------------------------------------------
  15. include::programs/io_input.py[]
  16. --------------------------------------------------
  17. Output:
  18. --------------------------------------------------
  19. include::programs/io_input.txt[]
  20. --------------------------------------------------
  21. .How It Works
  22. We use the slicing feature to reverse the text. We've already seen how we can make
  23. <<sequence,slices from sequences>> using the `seq[a:b]` code starting from position `a` to position
  24. `b`. We can also provide a third argument that determines the _step_ by which the slicing is
  25. done. The default step is `1` because of which it returns a continuous part of the text. Giving a
  26. negative step, i.e., `-1` will return the text in reverse.
  27. The `raw_input()` function takes a string as argument and displays it to the user. Then it waits
  28. for the user to type something and press the return key. Once the user has entered and pressed the
  29. return key, the `raw_input()` function will then return that text the user has entered.
  30. We take that text and reverse it. If the original text and reversed text are equal, then the text
  31. is a http://en.wiktionary.org/wiki/palindrome[palindrome].
  32. ==== Homework exercise
  33. Checking whether a text is a palindrome should also ignore punctuation, spaces and case. For
  34. example, "Rise to vote, sir." is also a palindrome but our current program doesn't say it is. Can
  35. you improve the above program to recognize this palindrome?
  36. If you need a hint, the idea is that... footnote:[Use a tuple (you can find a list of _all_
  37. http://grammar.ccc.commnet.edu/grammar/marks/marks.htm[punctuation marks here]) to hold all the
  38. forbidden characters, then use the membership test to determine whether a character should be
  39. removed or not, i.e. forbidden = (`!`, `?`, `.`, ...).]
  40. === Files
  41. You can open and use files for reading or writing by creating an object of the `file` class and
  42. using its `read`, `readline` or `write` methods appropriately to read from or write to the
  43. file. The ability to read or write to the file depends on the mode you have specified for the file
  44. opening. Then finally, when you are finished with the file, you call the `close` method to tell
  45. Python that we are done using the file.
  46. Example (save as `io_using_file.py`):
  47. [source,python]
  48. --------------------------------------------------
  49. include::programs/io_using_file.py[]
  50. --------------------------------------------------
  51. Output:
  52. --------------------------------------------------
  53. include::programs/io_using_file.txt[]
  54. --------------------------------------------------
  55. .How It Works
  56. First, open a file by using the built-in `open` function and specifying the name of the file and
  57. the mode in which we want to open the file. The mode can be a read mode (`'r'`), write mode (`'w'`)
  58. or append mode (`'a'`). We can also specify whether we are reading, writing, or appending in text
  59. mode (`'t'`) or binary mode (`'b'`). There are actually many more modes available and `help(open)`
  60. will give you more details about them. By default, `open()` considers the file to be a 't'ext file
  61. and opens it in 'r'ead mode.
  62. In our example, we first open the file in write text mode and use the `write` method of the file
  63. object to write to the file and then we finally `close` the file.
  64. Next, we open the same file again for reading. We don't need to specify a mode because 'read text
  65. file' is the default mode. We read in each line of the file using the `readline` method in a
  66. loop. This method returns a complete line including the newline character at the end of the
  67. line. When an _empty_ string is returned, it means that we have reached the end of the file and we
  68. 'break' out of the loop.
  69. In the end, we finally `close` the file.
  70. Now, check the contents of the `poem.txt` file to confirm that the program has indeed written to
  71. and read from that file.
  72. [[pickle]]
  73. === Pickle
  74. Python provides a standard module called `pickle` using which you can store _any_ plain Python
  75. object in a file and then get it back later. This is called storing the object *persistently*.
  76. Example (save as `io_pickle.py`):
  77. [source,python]
  78. --------------------------------------------------
  79. include::programs/io_pickle.py[]
  80. --------------------------------------------------
  81. Output:
  82. --------------------------------------------------
  83. include::programs/io_pickle.txt[]
  84. --------------------------------------------------
  85. .How It Works
  86. To store an object in a file, we have to first `open` the file in __w__rite __b__inary mode and
  87. then call the `dump` function of the `pickle` module. This process is called _pickling_.
  88. Next, we retrieve the object using the `load` function of the `pickle` module which returns the
  89. object. This process is called _unpickling_.
  90. [[unicode]]
  91. === Unicode
  92. So far, when we have been writing and using strings, or reading and writing to a file, we have used
  93. simple English characters only. If we want to be able to read and write other non-English
  94. languages, we need to use the `unicode` type, and it all starts with the character `u`:
  95. --------------------------------------------------
  96. >>> "hello world"
  97. 'hello world'
  98. >>> type("hello world")
  99. <type 'str'>
  100. >>> u"hello world"
  101. u'hello world'
  102. >>> type(u"hello world")
  103. <type 'unicode'>
  104. --------------------------------------------------
  105. We use the `unicode` type instead of `strings` to make sure that we handle non-English languages in
  106. our programs. However, when we read or write to a file or when we talk to other computers on the
  107. Internet, we need to convert our unicode strings into a format that can be sent and received, and
  108. that format is called "UTF-8". We can read and write in that format, using a simple keyword
  109. argument to our standard `open` function:
  110. [source,python]
  111. --------------------------------------------------
  112. include::programs/io_unicode.py[]
  113. --------------------------------------------------
  114. .How It Works
  115. You can ignore the `import` statement for now, we'll explore that in detail in the <<module,modules
  116. chapter>>.
  117. Whenever we write a program that uses Unicode literals like we have used above, we have to make
  118. sure that Python itself is told that our program uses UTF-8, and we have to put `# encoding=utf-8`
  119. comment at the top of our program.
  120. We use `io.open` and provide the "encoding" and "decoding" argument to tell Python that we are
  121. using unicode, and in fact, we have to pass in a string in the form of `u""` to make it clear that
  122. we are using Unicode strings.
  123. You should learn more about this topic by reading:
  124. - http://www.joelonsoftware.com/articles/Unicode.html["The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets"]
  125. - http://docs.python.org/2/howto/unicode.html[Python Unicode Howto]
  126. - http://nedbatchelder.com/text/unipain.html[Pragmatic Unicode talk by Nat Batchelder]
  127. === Summary
  128. We have discussed various types of input/output, about file handling, about the pickle module and
  129. about Unicode.
  130. Next, we will explore the concept of exceptions.