[英]Using Python to parse CSV to find specific string
Completely new to python (and programming).对python(和编程)来说是全新的。 Trying to write a python script that reads a CSV file and searches for a specific string.尝试编写一个 python 脚本来读取 CSV 文件并搜索特定字符串。 The string represents an instance which will eventually aid in a larger script (performing additional task).该字符串代表一个实例,它最终将有助于一个更大的脚本(执行额外的任务)。 With the script below I can read the CSV but I don't know how I can get the script to look for a specific string:使用下面的脚本,我可以读取 CSV,但我不知道如何让脚本查找特定字符串:
import csv
with open('XXXXXXX.csv', 'r') as csv_file:
csv_reader = csv.reader(csv_file)
for line in csv_reader:
print(line)
I have tried using splits, append, pandas and other options but I am not able to get it to work.我曾尝试使用拆分、附加、熊猫和其他选项,但我无法让它工作。 Would appreciate any assistance.将不胜感激任何帮助。
The in
operator can help you determine if something is in another thing, as shown below: in
运算符可以帮助您确定某物是否在另一物中,如下所示:
for line in file:
if "desired string" in line:
print("It's here")
Some examples from IDLE: IDLE的一些例子:
>>> s = "this is a string"
>>> a = s.split()
>>> a
['this', 'is', 'a', 'string']
>>> t = (1, 3, 32, 4)
>>> 'is' in s
True
>>> 'is' in a
True
>>> 'is' in a[0]
True
>>> 'is' in t
False
>>> 1 in t
True
>>> 32 in t
True
I think the easiest way would be to just type the word in quotes and check in the file immediately without a loop:我认为最简单的方法是在引号中输入单词并立即签入文件而无需循环:
'and' in open(r'C:\Users\user\Desktop\something.csv').read().split()
gives: True
Or if you know what words you want to check, you can pass them in a list and check them with this code to categorise them in found and not found categories like this:或者,如果您知道要检查哪些单词,则可以将它们传递到列表中并使用此代码检查它们,以将它们分类为已找到和未找到的类别,如下所示:
li = ['area','keep','have','sky'] #make a list with the words you want to check
for i in li:
if i in open(r'C:\Users\user\Desktop\something.csv').read().split():
print('found:' + i)
else:
print('not found:' + i)
Which gives the following:这给出了以下内容:
found:area
found:keep
found:have
not found:sky
Or a third way that looks more like your code and also counts how many times it is found:或者第三种方式看起来更像您的代码并且还计算找到它的次数:
import csv
with open(r'C:\Users\user\Desktop\something.csv', 'r') as csv_file:
csv_reader = csv.reader(csv_file)
z=0
ax=csv_file.read().split()
if 'and' in ax:
print('found')
for line in ax:
z+=line.count('and')
print(z)
Which gives:这使:
found
191
If the word is in the csv.如果单词在 csv 中。
You can search for a string in a CSV file and print the results.您可以在 CSV 文件中搜索字符串并打印结果。
import csv
# Asks for search criteria from user
search_parts = input("Enter search criteria:\n").split(",")
# Opens csv data file
file = csv.reader(open("C:\\your_path_here\\test.csv"))
# Go over each row and print it if it contains user input.
for row in file:
if all([x in row for x in search_parts]):
print(row)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.