Python搜索CSV文件并返回相关信息 [英] Python to search CSV file and return relevant info

查看:133
本文介绍了Python搜索CSV文件并返回相关信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个csv文件,其中包含有关我们网络上某些计算机的信息。我想能够从命令行输入一个快速行,带回我的相关项目从csv。格式为:

I have a csv file with information about some computers on our network. I'd like to be able to type from the command line a quick line to bring me back the relevant items from the csv. In the format:

$ tag.py *hostname*

csv有大约50列,信息范围从MAC地址到上次在网络上看到的时间。我只想在我搜索时输出这些列的选择。我写了必要的代码,它的工作原理。然而,我希望搜索更灵活。按照原样,搜索字词需要与我搜索的值完全相同。 aka

The csv has about 50 columns with information ranging from MAC address to last time seen on the network. I only want to output a selection of these columns when I search. I've written the necessary code and it works. However I want the search to be more flexible. As it stands, the search term needs to be exactly the same as the value I'm searching for. aka

$ tag.py mycomputer        # This returns nothing
$ tag.py mycomputer.co.uk  # This returns the information I want
$ tag.py 63746             # This returns nothing
$ tag.py 00063746          # This returns the information I want

现在对于我有的代码。

# Import Modules

import sys
import csv

# Get user Input
# I assume the script is used in the form script.py "search-term"
# If no input added to command, ask for user input

if len(sys.argv) < 2:
    print("Please enter a hostname or asset number.")
    search_1 = input("Search for:")
else:
    search_1=sys.argv[1]

# Setup Variables
# Open cvs and setup csv.reader settings

csvfile = open("file.csv", "r", encoding="Latin-1")
csvfile.seek 
reader = csv.reader(csvfile, dialect='excel', delimiter=",", quotechar="'")

# Search cvs for the input string

for line in reader:
    if search_1 in line:
        print("------------------------------------------------------")
        print("  Hostname = " + line[10])
        print("  " + line[11])
        print("  AssetId = " + line[30])
        print("  IP = " + line[7])
        print("  MAC = " + line[6])
        print("  Owner = " + line[15])
        print("  Username = " +line[14])
        print("  Tel = " + line[17])
        print("  Last Seen = " + line[27])
        print("------------------------------------------------------")

csvfile.close()



我希望代码能够忽略fqdn如果我搜索一个主机名或添加额外的0个字符到资产编号。我想我可以解决资产号问题与 len(search_1)< 8 在前面添加一些 0 ,直到它是8个字符长,但这避免了我真的更喜欢只搜索字符串

I would like the code to be able to ignore the fqdn if I search for a hostname or to add the extra 0 characters to the asset number. I think I can fix the asset number issue with a len(search_1) < 8 append some 0 to the front until it is 8 characters long but that's avoiding the fact that I really would prefer to just search for the string without manipulating it to match the thing I'm looking for.

推荐答案

而不是测试你的输入字符串是否在行中,测试如果您的输入字符串在任何列。 any()函数非常适用于:

Instead of testing if your input string is in the line, test if your input string is in any of the columns. The any() function is ideally suited for that:

if any(search_1 in col for col in line):

要分解一下: csv.reader() iterable本身是一个列的列表,你可以循环遍历这些列。 for col 就是这样。我们测试 search_1在col 中的每列中是否存在 search_1 ,以及 any 将执行循环,直到找到 search_1在中的 True 这种情况下它停止迭代循环并返回 True 本身。如果未找到匹配项,则返回 False

To break this down a little: each line in your csv.reader() iterable is itself a list of columns, you can loop over these. for col in line does just that. We test if search_1 is present in each column with search_1 in col, and any() will execute the loop until it finds a column where search_1 in col is True, in which case it stops iterating over the loop and returns True itself. If no match was found False is returned instead.

这篇关于Python搜索CSV文件并返回相关信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆