验证字符串是否在Python字典中作为键或值存在? [英] Verify string exists as key or value in Python dictionary?
问题描述
我正在为linux目录构建一个scraper / crawler。本质上该程序将用户输入起点(EX:/ home / user / Pictures /)和端点(EX:/ home / user / Pictures /)以及要刮的文件类型(这是我的问题所在in)
我将可接受的文件扩展名类型存储在具有嵌套列表的字典中,如下所示:
I'm building a scraper/crawler for linux directories. in essence the program will take users input for startpoint (EX: /home/user/Pictures/) and endpoint (EX: /home/user/Pictures/) as well as a file type to scrape for (which is where my question comes in)
I'm storing acceptable file extension types in a dictionary w/ nested lists like so:
file_types = {'audio': ['mp3', 'mpa'], 'images': ['png', 'jpg']}
如果我将用户输入存储为变量scrape_for如何验证变量scrape_for中的字符串是否存在于字典file_types中?
我尝试了什么:
这是我目前的代码块,它执行以下操作:
1.接受用户输入作为起点
2.验证startpoint是一个有效目录
3.取用户输入结束点
4.验证终点是两者一个有效的目录和起始点的子目录
5.打印文件扩展名选项供用户选择
if I store the users input as the variable scrape_for how can I validate that the string in the variable scrape_for exists in the dictionary file_types?
What I have tried:
this is my current block of code which does the following:
1. take user input for start point
2. verify startpoint is a valid directory
3. take user input for end point
4. validate end point is both a valid directory and sub directory of start point
5. print options of file extensions for user to choose from
import os
ftypes = {'audio': ['mp3', 'mpa', 'wpi', 'wav', 'wpi'], 'images': ['png', 'jpg', 'jpeg', 'gif', 'bmp'], 'text': ['txt', 'doc', 'pdf'], 'video': ['mp4', 'avi', '3g2', '3gp', 'mkv', 'm4v', 'mov', 'mpg', 'wmv', 'flv'], 'executable': ['apk', 'bat', 'bin', 'exe', 'py', 'wsf', 'com', 'cgi', 'pl']}
def UserInput():
#User inputs Start Point
Spoint = input('Where to start: \n')
#check validity of input
if os.path.isdir(Spoint):
print('Scraping will begin at: ' + Spoint)
elif not os.path.isdir(Spoint):
print('Not a valid directory')
exit()
#User input for End Point
Epoint = input('\n\nWhat directory would you like to stop scraping at? \n')
# Check if Endpoint is a valid SubDirectory of the parent directory
if os.path.isdir(Epoint) and len(Epoint) >= len(Spoint):
print('\n\nScraping will end at: ' + Epoint)
elif os.path.isdir(Epoint) or len(Epoint) >= len(Spoint):
print('Error w/ End Point directory, make sure directory is formatted correly, and is a sub directory of your Starting Point')
exit()
#User input for filetype
for k,v in ftypes.items():
print(k, v)
ScrapeType = input('Please enter The extension youd like scraped: \n')
推荐答案
假设您正在寻找文件类型'png'
True in [' png'in d for f in ftypes.values()]
将返回True
表示不存在的文件类型
真实的['mdi'在d中表示d在ftypes.values()]
将返回False
To检查钥匙
[ftypes.keys()中的'audio'将返回True
检查两者
[输入d表示d表示ftypes.values()]或输入ftypes.keys()
Suppose you are looking for a filetype 'png'
True in ['png' in d for d in ftypes.values()]
will return True
for a non-existent file type
True in ['mdi' in d for d in ftypes.values()]
will return False
To check for a key
['audio' in ftypes.keys()] will return True
To check both
True in [input in d for d in ftypes.values()] or input in ftypes.keys()
这篇关于验证字符串是否在Python字典中作为键或值存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!