我有工作脚本:
#argument passed to the script in order to indicate which item should be processed
searchedItem = sys.argv[1]
#get list of items with its details via REST API in json format
response = requests.get('http://localhost...')
vJsonStr = response.text
#convert json schema into summary list with names and ids only
fTxt = open(filepathTxt, 'w')
vJson = json.loads(vJsonStr)
for r in vJson['items']:
fTxt.write (r[ 'name' ] + ' ' + r[ 'id' ] + ' \n')
fTxt.close()
#check whether argument passed is covered by the list saved
file = open(filepathTxt, 'r')
content = file.read()
if searchedItem in content:
with open (filepathTxt, 'r') as file:
for line in file:
if searchedItem in line:
wantedLine = line.split(' ')
ItemName = wantedLine[0]
ItemId = wantedLine[1]
else:
print('WARNING: ...')
我的问题是:
如何用内置Python对象替换fTxt文件,这样我就可以摆脱文件并获得相同的最终结果?
下面表格中的fTxt文件允许我按项目搜索,并返回相应的id该练习的目的是什么。
第1项id1
第2项id2
项目3 id3
我应该使用口述还是列表? 如果有,应如何定义和更新/追加
目前没有回答
相关问题 更多 >
编程相关推荐