Pythons input and output Chenghao Wang Fancier Output

  • Slides: 12
Download presentation
 Python’s input and output Chenghao Wang

Python’s input and output Chenghao Wang

Fancier Output Formatting – Output method ▪ Print() ▪ Str() Example Difference between str(

Fancier Output Formatting – Output method ▪ Print() ▪ Str() Example Difference between str( ) and repr() ▪ Repr() The str() function is meant to return representations of values which are fairly human-readable. S=“Hello World” Print(s) Str(s) Repr(s) OR - ‘Hello World’ - “‘Hello World’” The repr() is meant to generate representations which can be read by the interpreter (or will force a Syntax. Error if there is no equivalent syntax). Print(“Hello World”) - Hello World

Fancier Output Formatting – Formatting Method ▪ str. rjust() ▪ str. ljust() ▪ str.

Fancier Output Formatting – Formatting Method ▪ str. rjust() ▪ str. ljust() ▪ str. center() The str. rjust() method can right-justifies a string in a field of a given width by padding it with spaces on the left. There are similar methods str. ljust() and str. center().

Fancier Output Formatting – Formatting Method ▪ str. zfill() This method returns the numeric

Fancier Output Formatting – Formatting Method ▪ str. zfill() This method returns the numeric string left filled with zeros in a string of length width. ▪ str. format() This method can replace the brackets and characters with the objects passed into. An optional ': ' and format specifier can follow the field name. Example 2 Example 3 import math '12'. zfill(5) print('We are the {} who print('This {food} is -> '00012' say {adjective}. '. format( print('The value of PI is "{}!"'. format('knights', 'Ni')). . . food='spam', approximately '-3. 14'. zfill(7) adjective='absolutely {0: . 3 f}. '. format(math. pi)) -> '-003. 14' -> We are the knights who horrible')) say "Ni!" ->The value of PI is '3. 14159265359'. zfill(5) ->This spam is absolutely approximately 3. 142. ->'3. 14159265359' horrible.

Fancier Output Formatting – Old Formatting Method ▪ The % operator can also be

Fancier Output Formatting – Old Formatting Method ▪ The % operator can also be used for string formatting. It interprets the left argument much like a sprintf()-style format string to be applied to the right argument, and returns the string resulting from this formatting operation. Example import math print('The value of PI is approximately %5. 3 f. ' % math. pi) ->The value of PI is approximately 3. 142.

Reading Files ▪ open(filename, mode) Mode ’r’ -> open the file only for reading

Reading Files ▪ open(filename, mode) Mode ’r’ -> open the file only for reading Mode ’w’ -> only writing (erase file with the same time) Mode ’a’ -> opens the file for appending Mode ’r+’ -> open the file for reading and writing Mode ‘b’ -> opens the file in binary mode Example f = open('workfile', 'w') f = open('filename', ‘r')

Reading Files ▪ f. read(size) This method reads some quantity of data and returns

Reading Files ▪ f. read(size) This method reads some quantity of data and returns it as a string or bytes object. When size is negative or omitted, then return the entire content; When the end of the file has been reached, then return empty string ‘’ Example f. read() ->'This is the entire file. n‘ f. read() ->'

Reading Files ▪ f. readline() This method reads a single line from the file;

Reading Files ▪ f. readline() This method reads a single line from the file; Example f. readline() ->'This is the first line of the file. n' f. readline() ->'Second line of the filen' f. readline() ->''

Writing Files ▪ f. write(string) This method writes the contents of string to the

Writing Files ▪ f. write(string) This method writes the contents of string to the file, returning the number of characters written. To write something other than a string, it needs to be converted to a string first. ▪ f. tell() This method returns an integer giving the file object’s current position Example value = ('the answer', 42) f. write('This is a testn') ->15 s = str(value) f. write(s) ->18

Writing Files ▪ f. seek(offset, from_what) The position is computed from adding offset to

Writing Files ▪ f. seek(offset, from_what) The position is computed from adding offset to a reference point; the reference point is selected by the from_what argument. A from_what value of 0 measures from the beginning of the file, 1 uses the current file position, and 2 uses the end of the file as the reference point, omitted means from the beginning of the file. ▪ f. close() After the writing is done, use this to close. Example f = open('workfile', 'rb+') f. write(b'0123456789 abcde f') ->16 f. seek(-3, 2) # Go to the 3 rd byte before the end ->13 f. read(1) ->b'd'

Saving structured data with json - Overview ▪ json Python allows users to use

Saving structured data with json - Overview ▪ json Python allows users to use the popular data interchange format called JSON (Java. Script Object Notation) to take Python data hierarchies, and convert them to string representations Another variant of the dumps() function, If you have an object x, you can view its To decode the object again, if f is a text JSON string representation with a simple called dump(), simply serializes the file object which has been opened for line of code: object to a text file. So if f is a text file reading: object opened for writing, we can do this: json. dumps([1, 'simple', x = json. load(f)) json. dump(x, f) 'list']) ->'[1, "simple", "list"]'

Works Citation The Python Tutorial https: //docs. python. org/3/tutorial/inputout put. html# Json’s Overview and

Works Citation The Python Tutorial https: //docs. python. org/3/tutorial/inputout put. html# Json’s Overview and python-related operations for json http: //www. cnblogs. com/coser/archive/20 11/12/14/2287739. html Thanks 5/21/2014