python判断字符是否是字典的键_验证字符串是否在Python字典中作为键或值存在?...

这段代码描述了一个Linux目录爬虫程序,它接受用户输入的起始和结束目录以及要抓取的文件类型。用户可以选择音频、图片、文本或视频等文件类型。目前的问题是如何验证用户输入的文件类型是否存在于预定义的字典中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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']}

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')

解决方案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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值