需求:
从某个 csv 表格提取需要的数据,写入 xlsx
如果第 11 列的单元格包含“xxx”字段,则删除该行
开始的时候,我是这样:
dir = (r'C:\\Users\\ShuoHui\\Desktop\\data\\')
workbook = open(dir + 'filename.csv')
workbookReader = csv.reader(workbook)
workbookData = list(workbookReader)
nRows = workbookReader.line_num
rowsNum = 1 # 行号
for rowsNum in range(1, nRows):
if workbookData[rowsNum][10] != 'xxx':
outsheet.write(rowsNum, 0, workbookData[rowsNum][4])
outsheet.write(rowsNum, 1, workbookData[rowsNum][10])
outsheet.write(rowsNum, 4, workbookData[rowsNum][9])
outsheet.write(rowsNum, 5, workbookData[rowsNum][2])
outsheet.write(rowsNum, 9, workbookData[rowsNum][0])
else:
continue
outworkbook.save(dir + "out\\filename.xlsx")
上面这种情况,可以输出想要的,但是会有空白行,于是我想说,加入一个 i 控制写入时候的行号,就是这样:
dir = (r'C:\\Users\\ShuoHui\\Desktop\\data\\')
workbook = open(dir + 'filename.csv')
workbookReader = csv.reader(workbook)
workbookData = list(workbookReader)
nRows = workbookReader.line_num
rowsNum = 1 # 行号
for rowsNum in range(1, nRows):
i = 1 #从第 2 行开始写入
if workbookData[rowsNum][10] != 'xxx':
outsheet.write(i, 0, workbookData[rowsNum][4])
outsheet.write(i, 1, workbookData[rowsNum][10])
outsheet.write(i, 4, workbookData[rowsNum][9])
outsheet.write(i, 5, workbookData[rowsNum][2])
outsheet.write(i, 9, workbookData[rowsNum][0])
i += 1
else:
continue
outworkbook.save(dir + "out\\filename.xlsx")
但是这样子,只会输出最后一行有效数据。
问题 1:以后我遇到这样循环的问题,怎么打断点排错(pycharm)
问题 2:我这个问题出在哪里。。。
ps:
*** 小弟基础不好 ***,请各位大神不吝赐教,感谢大家。