\r 回到行首
\n 换行
\r\n 回到行首并换行
在对文件进行读写操作时,发现\r和\n的效果一样
演示\r
f=open("try_rw2.txt","w+")
f.write("hello\rworld")
print(f.tell()) #输出当前文件读写指针的位置
f.seek(0,0) #把文件读写指针移到文件的开始
content=f.read()
print(content)
print(f.tell())
f.close()
结果:
演示\n
f=open("try_rw2.txt","w+")
f.write("hello\nworld")
print(f.tell()) #输出当前文件读写指针的位置
f.seek(0,0) #把文件读写指针移到文件的开始
content=f.read()
print(content)
print(f.tell())
f.close()
结果:
演示\r\n
f=open("try_rw2.txt","w+")
f.write("hello\r\nworld")
print(f.tell()) #输出当前文件读写指针的位置
f.seek(0,0) #把文件读写指针移到文件的开始
content=f.read()
print(content)
print(f.tell())
f.close()
结果:
从结果中看出\r和\n的显示效果一样都是回到行首,并且换行,这是因为在windows中\r和\n被自动转化为\r\n
而\r\n则转化为\r\n\r\n,如演示三
直接在控制台输出,\r,\n,\r\n显示的是它本身的意思
print("hello\rworld")
print("*"*30)
print("hello\nworld")
print("*"*30)
print("hello\r\nworld")
print("*"*30)
结果: