1.Python ASCII码与字符相互转换
#!/usr/bin/python3
# _*_ coding: UTF-8 _*_
str=input("Please input a char:")
num=input("Please input a ASCII:")
print('char ASCII:',ord(str))
print('ASCII char:',chr(num))
执行以上代码输出结果为:
Please input a char:'w'
Please input a ASCII:12
('char ASCII:', 119)
('ASCII char:', '\x0c')
2.Python 文件 IO
以下代码演示了Python基本的文件操作,包括 open,read,write:
#Write a file
with open("test.txt","wt") as out_file:
out_file.write("a b c d e")
#Read a file
with open("test.txt","rt") as in_file:
text=in_file.read()
print(text)
执行以上代码输出结果为:
a b c d e