如何从python中的CSV文件中删除空白行?

时间:2018-05-31 17:05:22

标签: python python-3.x csv

晚上好!我目前正在开发一个python 3脚本来创建一个包含指定数据列的CSV文件。我真的很接近完成它,但我遇到了一个我无法过去的问题。

基本上,我试图从两列中添加两个值(浮点数)并将它们附加到另一列;但是,当程序遇到空白行(字符串)时,一切都变得很糟糕。据我所知,将整个csv文件转换为浮点数是不可能的,所以我决定删除这些空行......我该怎么做?

另外,如果有人提出更清洁的方法,我会非常高兴听到它!

我的代码如下:

#! python3
# automatedReport.py - Reads and writes a new CSV file with
# Campaign Name, Group Name, Raised from Apr 1st-Apr 30th Total,
# Donation from Apr 1st-Apr 30th Total, and Campaign Total Apr 1st-Apr 30th.

import csv, os, ctypes
MessageBox = ctypes.windll.user32.MessageBoxW
total = 0

# Find out whether or not campaign_monthly_report is present.
if os.path.isfile('campaign_monthly_report.csv'):
    os.makedirs('automatedReport', exist_ok=True)
    print('Organizing campaign_monthly_report.csv...')

    #Read the CSV file.
    with open('campaign_monthly_report.csv', 'r') as csv_file:
        csv_reader = csv.DictReader(csv_file)

        #Write out the CSV file.
        with open(os.path.join('automatedReport', 'automated_campaign_monthly_report.csv'), 'w', newline='') as new_file:
            fieldnames = ['Campaign Name','Group Name','Raised from Apr 1st-Apr 30th Total','Donation from Apr 1st-Apr 30th Total', 'Campaign Total Apr 1st-Apr 30th']

            csv_writer = csv.DictWriter(new_file, fieldnames=fieldnames)

            csv_writer.writeheader()

            #Sloopy code, I know. I'm a bit new to this.
            for line in csv_reader:
                del line['Contact Name']
                del line['Contact Phone']
                del line['Contact Email']
                del line['Sign Ups']
                del line['Active Members']
                del line['% Active Members']
                del line['Raised upto Mar 31st 1st Time']
                del line['Raised upto Mar 31st Everyday']
                del line['Raised upto Mar 31st Total']
                del line['Raised upto Mar 31st Target']
                del line['Donation upto Mar 31st Group']
                del line['Donation upto Mar 31st PF']
                del line['Donation upto Mar 31st Total']
                del line['Additional $ Applied upto Mar 31st']
                del line['Raised from Apr 1st-Apr 30th 1st Time']
                del line['Raised from Apr 1st-Apr 30th Everyday']
                del line['Raised from Apr 1st-Apr 30th Target']
                del line['Donation from Apr 1st-Apr 30th Group']
                del line['Donation from Apr 1st-Apr 30th PF']
                del line['Additional $ Applied from Apr 1st-Apr 30th Total']
                del line['Date Joined']

                total = float(line['Raised from Apr 1st-Apr 30th Total']) + float(line['Donation from Apr 1st-Apr 30th Total'])
                csv_writer.writerow(line)
                print (total)

            MessageBox(None, 'Process Complete. Locate ouput in the automatedReport folder.', ' Success!', 0)
else:
    MessageBox(None, 'campaign_monthly_report not found!', ' Error!', 0)

错误讯息:

Traceback (most recent call last):
File "C:\Users\Mende\Desktop\Automated Campaign\automatedReport.py", line 51, in <module>
total = float(line['Raised from Apr 1st-Apr 30th Total']) + float(line['Donation from Apr 1st-Apr 30th Total'])
ValueError: could not convert string to float: >>> 

1 个答案:

答案 0 :(得分:0)

csv模块只希望其文件是一个迭代器,在每次迭代时返回一个新行。定义一个过滤掉空行的迭代器是很容易的:

def no_blank(fd):
    try:
        while True:
            line = next(fd)
            if len(line.strip()) != 0:
                yield line
    except:
        return

您可以使用它来过滤原始文件对象中的空行:

...
#Read the CSV file.
with open('campaign_monthly_report.csv', 'r') as csv_file:
    csv_reader = csv.DictReader(no_blank(csv_file))
    ...