简体   繁体   English

使用Python从Excel搜索和提取数据

[英]Use Python to search and pull data from Excel

import csv

subject = ['emergency*', 'new ticket*', 'problem with*']
from_to = ['chris*', 'timothy*', 'daniel*', 'david*', 'jason*']

a = open('D:\testfile.csv', 'w')

New to python. python新手。 So, here's what I'd like to do. 所以,这就是我想要做的。

1) Open an excel csv file 1)打开一个Excel的CSV文件

2) Search for specific keywords that are in a list 2)搜索列表中的特定关键字

3) If the keywords are found, pull the data that is in the D,E,F columns only. 3)如果找到了关键字,则仅提取D,E,F列中的数据。 (Since that is where the keywords will be) (因为这就是关键字所在的位置)

4) Write this data to a new file 4)将此数据写入新文件

Example. 例。 Search testfile.csv for any of the keywords in the from_to list. 在testfile.csv中搜索from_to列表中的任何关键字。 If these keywords appear ONLY in the D or E columns of excel AND if the corresponding column F is not equal to the subject list, then write a new file that has the columns of D,E,F and the associated lines, however many there are, with it 如果这些关键字仅出现在excel的D或E列中,并且如果对应的F列不等于主题列表,则编写一个新文件,该文件具有D,E,F列和相关行,但其中有很多行是,有

Also, I put the stars next to the names/items in the list to denote a wildcard, eg if the from_to contains chris.gmail.com or daniel@yahoo. 另外,我将星星放在列表中的名称/项目旁边,以表示通配符,例如,如果from_to包含chris.gmail.com或daniel @ yahoo。

This solution has a list of keywords, an input file that you read line by line, tokenize each line into a list of strings, then iterate your keyword list to check if any of them is in the tokenized line. 此解决方案有一个关键字列表,一个输入文件,您需要逐行读取该输入文件,将每行标记为字符串列表,然后迭代关键字列表以检查其中是否有任何标记。 If any keyword is found it writes the line to the output file. 如果找到任何关键字,它将把该行写到输出文件。

keywrds = ["word1", "word2", "etc"]
with open ("myfile.csv") as fin:
    with open ("outfile.txt") as fout:
        for line in fin:
            line_tokens = line.split(",")
            for word in keywrds:
                if word in line_tokens:
                    fout.write(line_tokens[3:6].join(" ") + "\n")

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM