python3去除列表中的空字符和\n
有时候在解析数据出来的时候总是会出现空格,回车\n,制表符\t,或者空,可尝试以下操作:
>>> list1 = ['hello,world!', ' ', '\n', '\t', '', 'I am here.'] >>> [x.strip() for x in list1 if x.strip()!=''] ['hello,world!', 'I am here.'] >>>
有时候在解析数据出来的时候总是会出现空格,回车\n,制表符\t,或者空,可尝试以下操作:
>>> list1 = ['hello,world!', ' ', '\n', '\t', '', 'I am here.'] >>> [x.strip() for x in list1 if x.strip()!=''] ['hello,world!', 'I am here.'] >>>