how to skip blank line while reading CSV file using python
这是我的代码,我能够打印每行,但是当出现空白行时,它会打印;由于CSV文件格式,因此当空白行出现时我想跳过
1 2 3 4 5 6 7 8 9 10 | import csv import time ifile = open ("C:\\Users\\BKA4ABT\\Desktop\\Test_Specification\ DBI.csv","rb") for line in csv.reader(ifile): if not line: empty_lines += 1 continue print line |
如果要跳过所有空格行,则应使用以下测试:
由于您可能要做的事情不只是将非空白行打印到控制台(不需要使用CSV模块),因此这里是一个涉及DictReader的示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | #!/usr/bin/env python # Tested with Python 2.7 # I prefer this style of importing - hides the csv module # in case you do from this_file.py import * inside of __init__.py import csv as _csv # Real comments are more complicated ... def is_comment(line): return line.startswith('#') # Kind of sily wrapper def is_whitespace(line): return line.isspace() def iter_filtered(in_file, *filters): for line in in_file: if not any(fltr(line) for fltr in filters): yield line # A dis-advantage of this approach is that it requires storing rows in RAM # However, the largest CSV files I worked with were all under 100 Mb def read_and_filter_csv(csv_path, *filters): with open(csv_path, 'rb') as fin: iter_clean_lines = iter_filtered(fin, *filters) reader = _csv.DictReader(iter_clean_lines, delimiter=';') return [row for row in reader] # Stores all processed lines in RAM def main_v1(csv_path): for row in read_and_filter_csv(csv_path, is_comment, is_whitespace): print(row) # Or do something else with it # Simpler, less refactored version, does not use with def main_v2(csv_path): try: fin = open(csv_path, 'rb') reader = _csv.DictReader((line for line in fin if not line.startswith('#') and not line.isspace()), delimiter=';') for row in reader: print(row) # Or do something else with it finally: fin.close() if __name__ == '__main__': csv_path ="C:\\Users\\BKA4ABT\\Desktop\\Test_Specification\ DBI.csv" main_v1(csv_path) print('\ '*3) main_v2(csv_path) |
而不是
1 | if not line: |
这应该起作用:
1 | if not ''.join(line).strip(): |
您始终可以检查逗号分隔值的数量。它似乎更具生产力和效率。
当迭代读取行时,由于这些是用逗号分隔的值的列表,因此您将获得一个列表对象。因此,如果没有元素(空白链接),则可以使其跳过。
1 2 3 4 5 | with open(filename) as csv_file: csv_reader = csv.reader(csv_file, delimiter=",") for row in csv_reader: if len(row) == 0: continue |
我的建议是只使用可以将文件分隔成行的csv阅读器。这样,您可以只检查行是否为空,如果是,则继续。
1 2 3 4 5 6 7 8 9 10 11 | import csv with open('some.csv', 'r') as csvfile: # the delimiter depends on how your CSV seperates values csvReader = csv.reader(csvfile, delimiter = '\\t') for row in csvReader: # check if row is empty if not (row): continue |
此示例仅以数组形式打印数据,同时跳过空行:
1 2 3 4 5 6 7 8 9 | import csv file = open("data.csv","r") data = csv.reader(file) for line in data: if line: print line file.close() |
我发现它比其他提供的示例更加清晰。
1 2 3 4 5 6 7 8 9 10 11 12 | import csv with open('userlist.csv') as f: reader = csv.reader(f) user_header = next(reader) # Add this line if there the header is user_list = [] # Create a new user list for input for row in reader: if any(row): # Pick up the non-blank row of list print (row) # Just for verification user_list.append(row) # Compose all the rest data into the list |
您可以删除开头和结尾的空格,如果该行之后的长度为零,则该行为空。
1 2 3 4 5 6 7 8 9 | import csv ifile=csv.reader(open('C:\\Users\\BKA4ABT\\Desktop\\Test_Specification\ DBI.csv', 'rb'),delimiter=';') for line in ifile: if set(line).pop()=='': pass else: for cell_value in line: print cell_value |