删除CSV文件Python中的空白条目 [英] Delete blank entries in CSV file Python

查看:39
本文介绍了删除CSV文件Python中的空白条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从Excel读取CSV文件,该文件的行可以是任意长度.

I need to read in a CSV file, from Excel, whose rows may be an arbitrary length.

问题是python保留了这些空白条目,但是需要删除它们以用于将来的算法.下面是输出,我不要空白条目.

The problem is the python retains these blank entries, but need to delete them for a future algorithm. Below is the output, I don't want the blank entries.

     ['5', '1', '5', '10', '4', '']
     ['3', '1', '5', '10', '2', '']
     ['6', '1', '5', '10', '5', '2']
     ['9', '10', '5', '10', '7', '']
     ['8', '5', '5', '10', '7', '']
     ['1', '1', '5', '10', '', '']
     ['2', '1', '5', '10', '1', '']
     ['7', '1', '5', '10', '6', '4']
     ['4', '1', '5', '10', '3', '1']

推荐答案

以下是与 csv 库集成的列表理解:

Here's a list comprehension integrated with the csv library:

import csv

with open('input.csv') as in_file:
    reader = csv.reader(in_file)
    result = [[item for item in row if item != ''] for row in reader]

print result

这篇关于删除CSV文件Python中的空白条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆