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'
- Python--Input Output,Unicode
- Python input and output
- Python[十二]:File Input output
- File Input/Output in Python
- Python 入门教程 18 ---- File Input/Output
- [Python]Input/Output and str Formatting
- Python Intro - System Input and Output
- I/O of python (INPUT / OUTPUT)
- The Python Tutorial - Input and Output
- Codeacademy Python-12-File Input/Output
- Python IO流(Input&Output)
- input/output
- The Python Tutorial 7——Input and Output
- Python 3.3 Tutorial Notes - 5:Input and Output
- [译]The Python Tutorial#7. Input and Output
- input and output
- Input和Output
- Redirection on input/output.
- 存储走向何处?听听希捷怎么说
- 代码风格-运算符两侧添加空格的规则(摘自百度百科)
- MongoDB 复制集
- 腾讯电脑管家首发“肉鸡”检测工具 力挫暗云III大规模DDos攻击阴谋
- 人工智能不仅能参加高考,百度还用它来帮你估分填志愿
- Python--Input Output,Unicode
- Recyclerview滚动实现隐藏和显示悬浮按钮
- JDBCUtil
- 安装CAJViewer时遇到注册模块出现1904错误解决方法
- 曾经我创办了个公司,然后被投资人踢出去了......
- leetcode 686. Repeated String Match
- 腾讯诉OPPO不正当竞争,已有经销商被责令禁售OPPO手机
- 新的开始
- Struts2乱码终极解决办法