简体   繁体   English

如何使用python检查CSV文件的特定列中是否存在列表中的字符串?

[英]How to check if a string in a list is present in a particular column in a CSV file using python?

I tried the following here target_conditions is the list which is to be compared with a column in PLM 我在这里尝试了以下target_conditions是要与PLM中的列进行比较的列表

csv file
with open('PLM.csv', 'rt') as f: 
     reader = csv.reader(f, delimiter=',')
     for row in reader:
        for str in target_conditions: 
             str=str.split(',')
             if str in row[3]: 
              print ("is in file") #but i need the patient name to be displayed

I have modified the code to find the value of target_conditions list from the column in PLM.csv file. 我修改了代码以从PLM.csv文件中的列中找到target_conditions列表的值。

with open('PLM.csv', 'rt') as csvfile: 
     reader = csv.reader(csvfile, delimiter=',')
     for row in reader:
        for str in target_conditions:
            if str in row:
                print(row[row.index(str)])

I hope this will help you. 我希望这能帮到您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 python 检查文件夹中是否存在特定文件 - Check if a particular file is present in the folder using python 如何检查Python中的列表中是否存在DataFrame字符串列的第一个单词? - How to check if first word of a DataFrame string column is present in a List in Python? 如何使用Python检查CSV文件中是否已存在数据 - How to check if an data already present in a CSV file using Python 如何使用特定列使用python对csv文件中的数据进行排序 - how to sort data in csv file using python using particular column 如何使用熊猫或普通python覆盖csv文件的特定列? - How to overwrite a particular column of a csv file using pandas or normal python? Python 在写入 CSV 文件之前检查列数据是否存在 - Python before writing to CSV file check if column data is present Python:如何使用列表检查csv列中是否没有值 - Python: How to check if there is no value in a csv column using a list 如何使用python将存在于多个数据框中的特定列写入列表? - How to write one particular column present in multiple dataframe to a list using python? 如何使用python将列表或字符串写入特定列的csv文件? - How I can write a list or string to a specific column csv file by using python? 如果Python的文本文件中没有特定的字符串,如何打印内容? - How to print something if a particular string is not present in a text file in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM