Python:使用参数解析过滤If语句
这里是Python新手。我有一个文本文件Python:使用参数解析过滤If语句,python,Python,这里是Python新手。我有一个文本文件 Name_1_Theo Coordinate_REAL Evergreen Coordinate_1_728362 House Apartment Age_15 Name_2_Sam Coordinate_REAL Slyvia Coordinate_2_839263 House Bungalow Age_13 以下是我当前的代码: import argparse with open ('stacknamecoordinate.txt','r')
Name_1_Theo
Coordinate_REAL
Evergreen
Coordinate_1_728362
House
Apartment
Age_15
Name_2_Sam
Coordinate_REAL
Slyvia
Coordinate_2_839263
House
Bungalow
Age_13
以下是我当前的代码:
import argparse
with open ('stacknamecoordinate.txt','r') as readfile:
parser = argparse.ArgumentParser(description='Filename to Script')
parser.add_argument ('--input_category', help='Input the category')
args = parser.parse_args()
name = 'Name'
house = 'House'
real_coordinates = 'Coordinate_REAL'
coordinate = 'Coordinate'
age = 'Age'
dict1={}
maindict={}
nextline=''
for lines in readfile:
if name in lines:
dict1['Name'] = lines.strip().split('_')[-1]
if house in lines:
nextline = next(readfile).strip()
dict1['House']=nextline
if real_coordinates in lines:
nextline = next(readfile).strip()
dict1['RealCoordinate'] = nextline
if coordinate in lines:
dict1['Coordinate1'] = lines.split('_')[-1].strip()
if age in lines:
dict1['Age'] = lines.strip().split('_')[-1]
maindict[dict1['Name']]=dict1
dict1={}
问题:
{'Sam': {'Age': '13',
'Name': 'Sam',
'RealCoordinate': 'Slyvia'},
'Theo': {'Age': '15',
'Name': 'Theo',
'RealCoordinate': 'Evergreen'}}
无论我称之为哪一类,捕获物、名字和年龄都会一直存在。不知道该怎么做,正则表达式还是布尔标志?
非常感谢 这可能就是你要找的。文件名有一个输入参数,要包含的类别有一个输入参数,这应该是逗号分隔列表中的确切类别 Cli: 脚本:
import re
import argparse
parser = argparse.ArgumentParser(description='Script to parse a list of words')
parser.add_argument('-c', '--categories',
type=lambda s: re.split(',| ,', s),
required=True,
help='Comma delimited list of categories')
parser.add_argument('-f', '--file',
required=True,
help='Name of the file')
args = parser.parse_args()
with open(args.file, 'r') as file:
lines = file.readlines()
d = {}
records = {}
for index, line in enumerate(lines):
if 'Name' in line:
d['Name'] = line.strip().split('_')[-1]
if 'House' in line and 'House' in args.categories:
d['House'] = lines[index + 1]
if 'Coordinate_REAL' in line and 'Coordinate_REAL' in args.categories:
d['RealCoordinate'] = lines[index + 1]
if 'Coordinate' in line and 'Coordinate' in args.categories:
d['Coordinate1'] = line.split('_')[-1].strip()
if 'Age' in line:
d['Age'] = line.strip().split('_')[-1]
records[d['Name']] = d
print(records)
您的终端中使用的命令示例是什么?python exercise.py中的
real\u坐标是什么?@IMCoins Hi!谢谢你的回复,哦,我在用pycharm。如果要筛选,我必须在终端中键入exercise.py--input_category(variable)。只需键入exercise.py
即可提供所有值。Real_坐标来自我调用的变量,Real_坐标='Coordination_Real',因此我可以调用的可能变量是姓名、Real_坐标、年龄、坐标或房子。非常感谢!!代码运行良好,但是否可以对类别使用可选参数?如果我输入exercise.py-f stacknamecordinate.txt
,它将输出整个字典值。还有,输出中是否可以像往常一样有“Name”和“Age”,而不必调用它?例如:exercise.py--categories'House'--file stacknamecordination.txt
输出:Key:Name,Age,House
Yep,这是通过不检查args.categories中的年龄和名称来完成的。相应地更改了代码。玩得高兴
import re
import argparse
parser = argparse.ArgumentParser(description='Script to parse a list of words')
parser.add_argument('-c', '--categories',
type=lambda s: re.split(',| ,', s),
required=True,
help='Comma delimited list of categories')
parser.add_argument('-f', '--file',
required=True,
help='Name of the file')
args = parser.parse_args()
with open(args.file, 'r') as file:
lines = file.readlines()
d = {}
records = {}
for index, line in enumerate(lines):
if 'Name' in line:
d['Name'] = line.strip().split('_')[-1]
if 'House' in line and 'House' in args.categories:
d['House'] = lines[index + 1]
if 'Coordinate_REAL' in line and 'Coordinate_REAL' in args.categories:
d['RealCoordinate'] = lines[index + 1]
if 'Coordinate' in line and 'Coordinate' in args.categories:
d['Coordinate1'] = line.split('_')[-1].strip()
if 'Age' in line:
d['Age'] = line.strip().split('_')[-1]
records[d['Name']] = d
print(records)