源文件每行后面都有回车,所以用下面输出时,中间会多了一行 try: with open("F:\\hjt.txt" ) as f : for line in f: print(line) except FileNotFoundError: print("读取文件出错") 有两种方法处理: 1.print后面带 end='',表示不换行 try: with open("F:\\hjt.txt" ) as f : for line in f: pri
说明:过滤换行符确实有点吭,不是那么好弄.处理文本时需要将换行符替换为空格,若使用sed命令会比较麻烦,而使用tr命令非常方便. 输入文本(country.txt) China America France German sed命令 cat country.txt | sed ':label;N;s/\n/ /;b label' tr命令 cat country.txt | tr "\n" " " 两个命令输出一致,但是sed命令的输出结尾有换行符,而tr命令的输出
6 PEP 278: Universal Newline Support The three major operating systems used today are Microsoft Windows, Apple's Macintosh OS, and the various Unix derivatives. A minor irritation of cross-platform work is that these three platforms all use different