亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Python 使用字典列表?我用什么

Python 使用字典列表?我用什么

米脂 2023-02-15 15:07:54
我正在做一種測驗,想知道如何將結果與文本文件進行比較。在根據提示輸入回答問題后,該函數將返回一個四位數代碼。我希望將該四位數代碼與我寫出的文本文件中的“truecode”進行比較,并提供如下附加信息:villagername,personality,birthday,zodiac,truecode,speciesAnkha,snooty,September 22nd,Virgo,A420,CatBangle,peppy,August 27th,Virgo,A330,TigerBianca,peppy,December 13th,Sagittarius,A320,TigerBob,lazy,January 1st,Capricorn,A210,CatBud,jock,August 8th,Leo,A310,Lion我希望打印出這些其他信息。    print("Your villager is " + villagername)    print("They are a " + personality + " type villagers and of the " + species + " species.")    print("Their birthday is " + birthday + " and they are a " + zodiac)    print("I hope you enjoyed this quiz!")我不知道如何提取這些信息并將其與我所擁有的進行比較。我應該使用列表還是字典?試圖用谷歌搜索我的問題并想知道我是否完全錯誤地解決了這個問題,我感到很沮喪。我如何將四位數代碼(將從另一個函數返回)與“真實代碼”進行比較,并像上面那樣吐出所有內容?
查看完整描述

3 回答

?
拉莫斯之舞

TA貢獻1820條經驗 獲得超10個贊

import csv



def get_data(filename):

    with open(filename) as f:

        reader = csv.DictReader(f, delimiter=',')

        data = {row['truecode']: row for row in reader}


    return data



def main():

    filename = 'results.txt'

    data = get_data(filename)


    code = input('Enter code: ')


    try:

        info = data[code]

        print("Your villager is " + info['villagername'])

        print("They are a " + info['personality'] +

              " type villagers and of the " + info['species'] + " species.")

        print("Their birthday is " +

              info['birthday'] + " and they are a " + info['zodiac'])

        print("I hope you enjoyed this quiz!")


    except KeyError:

        print('Invalid code')



if __name__ == "__main__":

    main()


查看完整回答
反對 回復 2023-02-15
?
繁星點點滴滴

TA貢獻1803條經驗 獲得超3個贊

import csv

def compare_codes(true_code):

    with open(''file.txt) as csvfile:

        details_dict = csv.reader(csvfile)

    for i in details_dict:

        if i['truecode'] == tru_code:

           print("Your villager is:",i['villagername'])

           print("They are a " + i['personality'] + " type villagers and of the " + i['species'] + " species.")

           print("Their birthday is " + i['birthday'] + " and they are a " + i['zodiac'])

           print("I hope you enjoyed this quiz!")

           break


compare_codes('A420')

上面的代碼讀取文本文件并將輸入與文件中的 truecode 值進行比較并顯示信息。


查看完整回答
反對 回復 2023-02-15
?
慕運維8079593

TA貢獻1876條經驗 獲得超5個贊

您擁有的文件類型實際上稱為 CSV 文件。如果愿意,您可以使用任何電子表格程序打開您的文本文件,您的數據將顯示在相應的單元格中。使用csv 模塊讀取數據。


import csv


def get_quiz_results(truecode):

    with open('your-text-file.txt') as csvfile:

        csvreader = csv.reader(csvfile)

        for row in csvreader:

            # row is a dictionary of all of the items in that row of your text file.

            if row['truecode'] == truecode:

                return row

然后打印出文本文件中的信息


truecode = 'A330'

info = get_quiz_results(truecode)

print("Your villager is " + info["villagername"])

print("They are a " + info["personality"] + " type villagers and of the " + info["species"] + " species.")

print("Their birthday is " + info["birthday"] + " and they are a " + info["zodiac"])

print("I hope you enjoyed this quiz!")

遍歷文件時,csv 模塊會將文件的每一行轉換為使用逗號作為分隔符的字典。第一行很特殊,用于創建字典鍵。


查看完整回答
反對 回復 2023-02-15
  • 3 回答
  • 0 關注
  • 163 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號