Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 提取CSV文件中不带';列表中没有元素_Python_Regex_List_Csv_Tokenize - Fatal编程技术网

Python 提取CSV文件中不带';列表中没有元素

Python 提取CSV文件中不带';列表中没有元素,python,regex,list,csv,tokenize,Python,Regex,List,Csv,Tokenize,我有一个带有子字符串的列表,如果列表中的任何子字符串出现在CSV文件的该列中,我需要将其与CSV文件中的一列进行比较。我想写那些在字符串列中没有子字符串的行。这个文件中有很多列,我只查看一列 示例my_字符串列具有值 { "This is just comparison of likely tokens","what a tough thing?"} de = ["just","not","really ", "hat"] 我只想写一行“多么艰难的事情?” 如果列中的列表中只有单词,则此操作

我有一个带有子字符串的列表,如果列表中的任何子字符串出现在CSV文件的该列中,我需要将其与CSV文件中的一列进行比较。我想写那些在字符串列中没有子字符串的行。这个文件中有很多列,我只查看一列

示例my_字符串列具有值

{ "This is just comparison of likely tokens","what a tough thing?"}

de = ["just","not","really ", "hat"]
我只想写一行“多么艰难的事情?”

如果列中的列表中只有单词,则此操作很好。例如,如果my_字符串列具有“真的”,它将不会写入新文件。但,若列表中的项带有其他字符串,则无法传递

with open(infile, 'rb') as inFile, open(outfile, 'wb') as outfile:
reader = csv.reader(inFile, delimiter=',')
writer = csv.writer(outfile, delimiter=',')

for row[1] in reader:

    if any(d in row[1] for d in de):
        pass
    else:
        writer.writerow(row[1])

为了检查子字符串列表中是否存在字符串,我通常使用集合

list1 = ['a','b','c']
list2 = ['c','d','e']
现在,为了找出区别

list3 = list(set(a) - set(b))
这就给了你['a','b'](列表1中的内容不在列表2中)并且你有你感兴趣的字符串。做

list(set(b) - set(a)) 

将为您提供“列表2中哪些内容不在列表1中?”的字符串,即['e','d']

听起来您想搜索单词,而不仅仅是子字符串,例如,“hat”与“what”不匹配。当需要匹配复数、不同大小写、连字符字符串等时,单词搜索可能会变得复杂。但是,如果您不介意忽略这些复杂情况,可以使用正则表达式将列分解为一个单词列表,将其小写,然后使用set操作进行检查

import re
import csv

# TEST: write a sample csv file. using col0 to indicate what should be
# in the outfile
open('infile.csv', 'w').write(
"""exclude,This is just a comparison of likely tokens,col02,col03
include,what a tough thing?,col12,col13""")

# the words to find
de = ["just","not","really", "hat"]

# the files
infile = 'infile.csv'
outfile = 'outfile.csv'

# a "normalized set" of words to search
de = set(word.lower() for word in de)

def normalize_text(text):
    """Return a set of all the words in lowercased text"""
    return set(re.findall('\w+', text.lower()))

with open(infile, 'r') as inFile, open(outfile, 'w') as outFile:
    reader = csv.reader(inFile, delimiter=',')
    writer = csv.writer(outFile, delimiter=',')
    for row in reader:
        mycol = normalize_text(row[1])
        if not mycol & de:
            writer.writerow(row)

print("---- output file ----")
print(open(outfile).read())

您可以将单词编译成单个正则表达式,甚至可以按如下方式进行不区分大小写的匹配:

r = re.compile('\\b('+"|".join(de)+')\\b', re.IGNORECASE)
那么您的代码可以是:

with open(infile, 'rb') as inFile, open(outfile, 'wb') as outfile:
reader = csv.reader(inFile, delimiter=',')
writer = csv.writer(outfile, delimiter=',')

for row in reader:
    if not r.search(row[1]):
        writer.writerow(row[1])

您能否发布CSV中的样本数据集和所需的输出?顺便说一句,如果性能确实重要,我建议使用pandas模块……您的示例的语法无效,并且没有定义
readrow
,但它类似于应该工作的脚本。你能发布一个有效的例子以及这个例子是如何失败的吗?@tdelaney谢谢,这是我的打字错误,因为我在尝试其他东西,却忘了改回去。这是原始代码,工作正常,但如果列表de中存在字符串和子字符串,则无法匹配。感谢您的回答,但两者都不是列表。而且,我不能这么做。一个是一行,长字符串值作为CSV文件中的列值之一显示。非常感谢。非常感谢你。QQ有没有一种方法可以让我在文字中加入一些符号。例如,我想将“really_tokens”与“really”匹配起来。非常感谢。是的,正则表达式可以是'r“[\w\u \-]”,例如包括斜杠和下划线。请注意原始字符串和反斜杠以转义在正则表达式中具有其他含义的字符。谢谢!这是把单词分成我不想要的字母。我想确保它只复制列表中不存在的字符串的所有列。非常感谢。NP我不知道你把单词分成字母是什么意思。正则表达式的形式为r'\b(just | not | really | hat)\b',它将匹配列表中的所有单词;“\b”是单词边界符号。你自己试试看,这就是我想做的。但不知何故,正则表达式行给了我无限的括号错误。我试图修复它,但仍然得到另一个错误-不支持|的操作数类型:'str'和'str';当我修改r=re.compile(“\\b”(“+”|“.join(de)+”)\\b”,re.IGNORECASE)时,我想你有一个额外的引号,用这个:r=re.compile(“\\b”(“+”|“.join(de)+”)\\b',re.IGNORECASE)我真的很抱歉,你是对的。是我的python笔记本编辑器出了问题。我用jupyter在另一台机器上试过,它工作得很好。这就是我在看的。回答得很好。非常感谢你。即使我做了很长一段路,我也会用这个。