在csv中搜索行/列无法正常工作(python)

时间:2017-03-10 15:45:57

标签: python csv

import csv
import time, collections, sys, openpyxl, xlrd

from openpyxl import Workbook
from openpyxl import load_workbook
from datetime import datetime

## Read the CSV file ##
scanReport = open('scan_report2.csv', 'r')
scanReader = csv.reader(scanReport)

desired_row = "Severity"
## search row's in csv - print out list ##
with open('csvtest.csv', 'w', newline='') as testfile:
    csv_writer = csv.writer(testfile)
    print("Checking for Severity Level's...")
    for row in scanReader:
        if desired_row == scanReader:
            print(row)
        else:
            print('Does not match')

"严重性"是我的csv文件中的文本 - 我可以打开它,但它没有搜索和打印我想要的列

1 个答案:

答案 0 :(得分:0)

您将字符串desired_row与读者对象进行比较,因此他们永远不会比较相等。从csvReader返回的每一行都是一个单元格值列表,所以请尝试以下方法:

for row in scanReader:
    for cell in row:
        if cell.strip() == "Severity":
            print(row)

这将匹配仅包含文本Severity

的单元格