从代码中学Python语法四(持续更新)

# -*- coding:utf-8 -*-
import math
import pickle
__author__ = 'hunterhug'
for x in range(1, 11):
    print(repr(x).rjust(4), repr(x*x).rjust(4), end=' ')
    # Note use of 'end' on previous line
    print(repr(x*x*x).rjust(4))

print(str("5656  56").rjust(5))


for x in range(1, 11):
    print('{0:3d}|{1:4d}|{2:4d}'.format(x, x*x, x*x*x))

print("dd".ljust(5),"|")
print(str("dd").ljust(5),"|")
print("dd".rjust(5),"|")
print("dd".center(5),"|")
print('3.14159265359'.zfill(5))
print('-3.11'.zfill(5))
print('-3.11'.zfill(7))
print('3.11'.zfill(5))

print('We are the {} who say "{}!"'.format('knights', 'Ni'))
print('We are the {0}{1} who say "{1}!"'.format('knights', 'Ni'))
print('This {food} is {adjective}.'.format(
    food='spam', adjective='absolutely horrible'))
print('This {0}{1}{food} is {adjective}.'.format(5,6,
    food='spam', adjective='absolutely horrible'))

# '!a' (应用 ascii()), '!s' (应用 str() ) 和 '!r' (应用 repr() )
# 可以在格式化之前转换值:

print('The value of PI is approximately {}.'.format(math.pi))
print('The value of PI is approximately {!r}.'.format(math.pi))
print('The value of PI is approximately {!s}.'.format(math.pi))
print('The value of PI is approximately {!a}.'.format(math.pi))

print('The value of PI is approximately {0:.3f}.'.format(math.pi))

table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 7678}
for name, phone in table.items():
    print('{0:10} ==> {1:10}'.format(name, phone))

# 字符: 数字:f :d
table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}
print('Jack: {0[Jack]}; Sjoerd: {0[Sjoerd]:d}; '
      'Dcab: {0[Dcab]:d}'.format(table))
print('Jack: {Jack:d}; Sjoerd: {Sjoerd:d};'
      ' Dcab: {Dcab:d}'.format(**table))

# 旧式
print('The value of PI is approximately %5.3f.' % math.pi)

# r模式文件不存在抛出错误
# 只有r+,读写模式

f = open('workfile.txt', 'r+')
print(str(f.read(1)))
print(str(f.read(3)))

# 如果到了文件末尾,f.read() 会返回一个空字符串(”“)。
# f.readline() 从文件中读取单独一行,字符串结尾会自动加上一个换行符( \n )
print(f.readline())
print(f.readline())
while f.readline()!='':
    print(f.readline())
f1 = open('work.txt', 'r+')
# print(f1.readlines())
for line in f1:
    print(line, end='\t')
print(f1.write('This is a test'))
value = ('the answer', 42)
s = str(value)
f1.write(s+"\n")


f2=open("t1.txt","r")
print("位置",f2.tell()) #指针位置
print(f2.read(1))
print("位置",f2.tell()) #指针位置
print(f2.read(2))
print("位置",f2.tell()) #指针位置

#使用 f.seek(offset,from_what) 。
# 指针在该操作中从指定的引用位置移动 offset 比特,
# 引用位置由 from_what 参数指定。
#  from_what 值为 0 表示自文件起始处开始,
# 1 表示自当前文件指针位置开始,2 表示自文件末尾开始。
#  from_what 可以忽略,其默认值为零,此时从文件头开始。
f2.seek(2)
print("位置",f2.tell()) #指针位置
f2.seek(0,1) # 从当前指针开始,第一位只能是0
print("位置",f2.tell()) #指针位置
f2.seek(0,2) # 从最后指针开始,第一位只能是0
print("位置",f2.tell()) #指针位置

# >>> f = open('/tmp/workfile', 'rb+')
# >>> f.write(b'0123456789abcdef')
# 16
# >>> f.seek(5)     # Go to the 6th byte in the file
# 5
# >>> f.read(1)
# b'5'
# >>> f.seek(-3, 2) # Go to the 3rd byte before the end
# 13
# >>> f.read(1)
# b'd'

f1.close()
print(f2.closed)
f2.close()

f3 = open("t2.txt","r+")
a = f3.read()
print(a)
f3.close()

# 自动关闭文件
with open('work.txt', 'r') as f:
    read_data = f.read()
print(read_data)

x = ["ff", "ff"]
ff = open("workf.txt","rb+")
# pickle.dump(x, ff)
# 不能同时写和读
x = pickle.load(ff)
print(x)

#http://docs.pythontab.com/python/python3.4/errors.html

 

你可能感兴趣的:(从代码中学Python语法四(持续更新))