Python--Input Output,Unicode

来源:互联网 发布:手机立体投影软件 编辑:程序博客网 时间:2024/05/16 12:11

Note: 因为从后往前学习,所以越到前面越基础,速度越快。Python第二部分记录library的学习,将会是重中之重。

7.Input and Output

7.1 Fancier Output Formatting

rper(),str():

  • 对于没有人类易读格式的对象,两个函数等同
  • 对于列表、字典等结构和成员两个函数效果相同
  • 对于字符串和浮点数字两个函数有明显的不同

format

  • 数字指示参数
  • 关键字指示参数
  • 混合使用数字和关键字指示参数
  • {:format specifier}:后面的可以指定输出格式
  • format对于字典的处理可以有两种方式
    • {[key]:format specifier}来访问
    • {key} format(**dict)
>>> dict={'one':1,'two':2,'three':3}>>> print ("{[two]}").format(dict)2>>> print ("{two}").format(**dict)2

7.2. Reading and Writing Files

linux和windows上对于2进制文件和文本文件的处理结果不同

Python on Windows makes a distinction between text and binary files; the end-of-line characters in text files are automatically altered slightly when data is read or written. This behind-the-scenes modification to file data is fine for ASCII text files, but it’ll corrupt binary data like that in JPEG or EXE files.

已知的一种区别就是换行符的区别,linux下的\n到windows下的\r\n。具体的更多以后补充

7.2.1. Methods of File Objects

read()与readline的区别

>>> f=open("asc.txt","rb")>>> f.read()'abcedf\n123456\n,\n\n'
>>> f.readline()'123456\n'>>> f.readline()',\n'>>> f.readline()'\n'>>> f.readline()''

asc.txt文件:

abcedf123456,

seek的使用:

seek(offset,from_what),from_what有三个值:

  • 0:the begining of the file
  • 1:current position
  • 2:end of the file

3.1. Using Python as a Calculator

Python also has built-in support for complex numbers, and uses the j or J suffix to indicate the imaginary part (e.g. 3+5j).

The print statement produces a more readable output, by omitting the enclosing quotes and by printing escaped and special characters:

If you don’t want characters prefaced by \ to be interpreted as special characters, you can use raw strings by adding an r before the first quote:

如果字符串不想\X被处理,使用r的前缀就可以做到。

3.1.3. Unicode Strings

这个问题比我想象的复杂的多,因为要理解什么是万国码:jobbole

The built-in function unicode() provides access to all registered Unicode codecs (COders and DECoders). Some of the more well known encodings which these codecs can convert are Latin-1, ASCII, UTF-8, and UTF-16. The latter two are variable-length encodings that store each Unicode character in one or more bytes. The default encoding is normally set to ASCII, which passes through characters in the range 0 to 127 and rejects any other characters with an error. When a Unicode string is printed, written to a file, or converted with str(), conversion takes place using this default encoding.

太过于复杂的概念不应该现在涉及,总结一下官网和以上的资料:

  • 字节和字符串的区别
  • encode和decode的区别
  • 自动转化

举几个例子,自己放在后面学习解释:

>>> u'我们'u'\u6211\u4eec'>>> u'我们'.encode('utf-8')'\xe6\x88\x91\xe4\xbb\xac'>>> print u'我们'我们>>> i=u'我们'.encode('utf-8')>>> i.decode('utf-8')u'\u6211\u4eec'
原创粉丝点击