简体   繁体   English

如何使用Python检查CSV文件中是否已存在数据

[英]How to check if an data already present in a CSV file using Python

Good day all, 大家好

I am trying to write a python script that picks up IP address(es) from a textfile, check if the IP addresses already present in CSV file, if IP address is already present then do nothing (pass), if the IP is not present in the CSV file then append the IP address in the CSV file. 我正在尝试编写一个从文本文件中获取IP地址的python脚本,检查CSV文件中是否已存在IP地址,如果已经存在IP地址,则不执行任何操作(通过),如果IP不存在在CSV文件中添加IP地址,然后在CSV文件中添加IP地址。

I can add the IP addresses to the CSV file but I am not able to get my head around with how to check if the IP address is already present in the file. 我可以将IP地址添加到CSV文件中,但是我无法理解如何检查文件中是否已存在IP地址。

if os.path.isfile('IP_file.txt'):
    logfile = open('IP_file.txt', 'r')
    csv_file = open('csv_file.csv', 'ab')
    for line in logfile:
        if line in csv_file: # This is not working
            pass
        else:
            csv_file.write(line)
    csv_file.close()
    logfile.close()
else:
    print('No file with filename ' logfile 'created')

I also tried to use the CSV module but no luck. 我也尝试使用CSV模块,但没有运气。 Any help is much appreciated. 任何帮助深表感谢。

CSV is just a text format. CSV只是一种文本格式。 Read it line by line, test every line against your IP. 逐行阅读,针对您的IP测试每一行。 Like 喜欢

ip="192.168.1.1"
for line in csv_file:
    if ip in line:
        found = True
        break

I am a bit concerned against your cycling over file on the disk. 我有点担心您对磁盘上的文件进行循环。 Probably it is better to read files into memories (find all IPs in CSV file first and put in a list) and then check against the list instead of opening and iterating over whole CSV file for every line of IP file. 最好将文件读入内存(首先在CSV文件中找到所有IP,然后放入列表中),然后对照该列表进行检查,而不是为IP文件的每一行打开并遍历整个CSV文件。

Keep a list or dict that holds the unique fetched values: 保留一个包含唯一获取的值的列表或字典:

found_address = []
with open('IP_file.txt', 'r') as logfile:
    with open('csv_file.csv', 'ab') as csv_file:
        for line in logfile:
            if not line in found_address:
                csv_file.write(line)
                found_address.append(line)

You are opening csv_file.csv in append mode ab . 您正在以追加模式ab打开csv_file.csv。 And then you are trying to check line in csv_file . 然后,您尝试检查line in csv_file In append-mode file-pointer points to the end-of-file. 在追加模式下,文件指针指向文件结尾。

little crazy functional-style python: 小小的疯狂的函数式python:

ip_file = set(map(str.strip, open('IP_file.txt').readlines()))

if CSV file is: 192.168.1.1,192.168.1.2,192.168.1.3,... 如果CSV文件是: 192.168.1.1,192.168.1.2,192.168.1.3,...

csv_file = set(map(str.strip, open('csv_file.csv').read().split(',')))
diff_ip = ip_file - csv_file
open('csv_file.csv','a').write(''.join(map(lambda x: ',{}'.format(x), list(diff_ip))))

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

相关问题 Python 在写入 CSV 文件之前检查列数据是否存在 - Python before writing to CSV file check if column data is present 如何使用python检查CSV文件的特定列中是否存在列表中的字符串? - How to check if a string in a list is present in a particular column in a CSV file using python? 如何使用Python检查数据是否在CSV文件中 - How to check whether the data is in a CSV file using Python 如何检查节点是否已存在于 Graphviz Python 中 - how to check if a node is already present in Graphviz Python 使用python将数据从csv复制到postgres之前,如何检查行已存在 - How to check a row already exists before copying the data from csv to postgres using python 如何检查CSV Python中是否存在用户输入 - How to check if User Input is present in CSV Python 如何检查 csv 文件中是否已经存在这样的行 - How to check if there is already such a line in a csv file 如何检查python中的文件夹中是否存在.dat文件,如果存在,如何将其与关联的csv匹配? - How to check if a .dat file is present in a folder in python and if it is there,how to match it with associated csv? 如何通过python在.yaml文件中存在的现有数据之间插入数据? - how to insert data in between already existing data present in .yaml file through python? 使用 python 检查文件夹中是否存在特定文件 - Check if a particular file is present in the folder using python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM