【发布时间】:2020-01-01 07:37:28
【问题描述】:
项目即将将短形式转换为长描述并从 csv 文件中读取
示例:用户输入 LOL,然后它应该响应“Laugh of Laughter”
期望:直到用户输入错误的关键字计算机一直要求输入短格式并且系统回答它来自 CSV 文件的长描述 我将 CSV 文件的每一行视为字典并分解为键和值 使用的逻辑: - 使用时,它会一直询问,直到短列没有找到空间,空单元格。但问题是在 IF 循环中显示成功的第一次尝试比较后没有发生,因为 readitems['short' ] 在每个循环中都没有得到更新
AlisonList.csv 值为:
short,long
lol,laugh of laughter
u, you
wid, with
import csv
from lib2to3.fixer_util import Newline
from pip._vendor.distlib.util import CSVReader
from _overlapped import NULL
READ = "r"
WRITE = 'w'
APPEND = 'a'
# Reading the CSV file and converted into Dictionary
with open ("AlisonList.csv", READ) as csv_file:
readlist = csv.DictReader(csv_file)
# Reading the short description and showing results
for readitems in readlist:
readitems ['short'] == ' '
while readitems['short'] !='' :
# Taking input of short description
smsLang = str(input("Enter SMS Language : "))
if smsLang == readitems['short']:
print(readitems['short'], ("---Means---"), readitems['long'])
else:
break
【问题讨论】:
-
感谢 Ed Ward。真的谢谢
标签: python loops dictionary if-statement