简体   繁体   English

如何在使用python读取CSV文件时跳过空行

[英]how to skip blank line while reading CSV file using python

This is my code i am able to print each line but when blank line appears it prints ;这是我的代码,我可以打印每一行,但是当出现空白行时,它会打印; because of CSV file format, so i want to skip when blank line appears由于 CSV 文件格式,所以我想在出现空行时跳过

import csv
import time

ifile = open ("C:\Users\BKA4ABT\Desktop\Test_Specification\RDBI.csv", "rb")
for line in csv.reader(ifile): 
    if not line: 
        empty_lines += 1 
        continue
    print line

If you want to skip all whitespace lines, you should use this test: ' '.isspace() .如果你想跳过所有空白行,你应该使用这个测试: ' '.isspace()

Since you may want to do something more complicated than just printing the non-blank lines to the console(no need to use CSV module for that), here is an example that involves a DictReader:由于您可能想做一些比将非空行打印到控制台更复杂的事情(不需要为此使用 CSV 模块),这里是一个涉及 DictReader 的示例:

#!/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\RDBI.csv"
    main_v1(csv_path)
    print('\n'*3)
    main_v2(csv_path)

Instead of代替

if not line:

This should work:这应该有效:

if not ''.join(line).strip():

You can always check for the number of comma separated values.您始终可以检查逗号分隔值的数量。 It seems to be much more productive and efficient.它似乎更具生产力和效率。

When reading the lines iteratively, as these are a list of comma separated values you would be getting a list object.迭代读取行时,由于这些是逗号分隔值的列表,因此您将获得一个列表对象。 So if there is no element (blank link), then we can make it skip.所以如果没有元素(空白链接),那么我们可以让它跳过。

        with open(filename) as csv_file:
          csv_reader = csv.reader(csv_file, delimiter=",")
          for row in csv_reader:
            if len(row) == 0:
                continue

my suggestion would be to just use the csv reader who can delimite the file into rows.我的建议是只使用可以将文件分隔成行的 csv 阅读器。 Like this you can just check whether the row is empty and if so just continue.像这样,您可以检查该行是否为空,如果是,则继续。

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

您可以去除前导和尾随空格,如果长度为零,则该行为空。

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

This example just prints the data in array form while skipping the empty lines:这个例子只是在跳过空行的同时以数组形式打印数据:

import csv

file = open("data.csv", "r")
data = csv.reader(file)

for line in data:
    if line: print line

file.close()

I find it much clearer than the other provided examples.我发现它比其他提供的示例更清晰。

import csv
ifile=csv.reader(open('C:\Users\BKA4ABT\Desktop\Test_Specification\RDBI.csv', 'rb'),delimiter=';')
for line in ifile:
    if set(line).pop()=='':
        pass
    else:
        for cell_value in line:
            print cell_value

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM