在python中处理空行时,经常会遇到一些问题.现总结经验如下: 1.遇到的空行如果只有换行符,直接使用=='\n'或者 len(line)==line.count('\n') 2.有多个空格+换行符时.有几种处理方法:①split: ②正则表达式'^\n'(不会):③if eachLine[:-1].strip() 展开: eg.文件过滤,显示一个文件的所有行,忽略以井号(#)开头的行. ① f=open('test.txt','r') for eachLine in f: if not ea
发现自己写python的空格split还挺多坎的,尤其是最后一个是空格的情形: def split(s): i = 0 ans = [] while i < len(s): start = i # find space while i < len(s) and s[i] != ' ': i += 1 ans.append(s[start:i]) i += 1 if s and s[-1] == " ": ans.append("") return ans
str8="中国 和 韩国 的区别" # a=str8.find("Python") # print a b=str8.find("和") print b word=str8.split(" ") #Python3 和Spark 这里可以直接正确分割中文 print word for i in word: #python 2.x 需要这样输出 print i #这是关于编码的问题# print "-"*50