1

我正在编写一个程序,该程序从 CSV 获取数据并将其转换为要导出为 PDF 的表格。我正在使用的 CSV 有一堆空行,所以当我在 Python 中创建矩阵时,我有一堆空行。我想删除所有以''开头的行。我写的代码是:

i=0
x=rows-empty ##where empty has been defined and the number of rows I need to delete.
for i in range(x):
    if Matrix[i][0] == '':
        del Matrix[i]
    i+=1

我遇到的问题是如果有两个连续的空行,只有一个被删除。关于如何摆脱两条线的任何想法?

我使用以下代码创建并填充矩阵:

##creates empty matrix
with open(filename) as csvfile:
serverinfo=csv.reader(csvfile, delimiter=",", quotechar="|")
rows=0

for row in serverinfo:    
    NumColumns = len(row)        
    rows += 1

Matrix=[[0 for x in xrange(9)] for x in xrange(rows)]  
csvfile.close()

##fills Matrix
with open(filename) as csvfile:
serverinfo=csv.reader(csvfile, delimiter=",", quotechar="|")
rows=0

for row in serverinfo:
    colnum = 0
    for col in row:
        Matrix[rows][colnum] = col
        if col==0:
            del col
        colnum += 1
    rows += 1
csvfile.close()
4

1 回答 1

0

不要在之后删除它们,而是不要从一开始就加载它们。您还可以快捷方式重新读取文件两次,因为您似乎9设置了列限制,因此对于每一行,只需用0's 将其填充到该大小......例如:

import csv
from itertools import chain, islice, repeat

COLS = 9 # or pre-scan file to get max columns
FILL_VALUE = 0 # or None, or blank for instance
with open(filename) as fin:
    csvin = csv.reader(fin) # use appropriate delimiter/dialect settings
    non_blanks = (row for row in csvin if row[0]) # filter out rows with blank 1st col
    matrix = [list(islice(chain(row, repeat(FILL_VALUE)), COLS)) for row in non_blanks] 

根据您对数据的处理方式,您可能还希望查看numpy模块和可用loadtxt()方法。

于 2013-06-17T20:56:49.303 回答