错误:从json文件中读取字典:解压缩的值太多

时间:2018-04-25 21:29:05

标签: python dictionary blackjack

请参阅以下错误消息和字典:

df <- read.table(text = "Column
'1,2,4,7,9,0'
'5,3,8,9,0'
'3,4'
'5.8,9,3.5'
'6'
NA
'7,4,3'",
header = TRUE, stringsAsFactors = FALSE)

当我使用以下代码时出现:

Traceback (most recent call last):
  File "C:/Users/richarddoi/Desktop/blackjack/test blackjack csv.py", line 9, in <module>
    for key,value in data:
ValueError: too many values to unpack (expected 2)

{'Ace of Hearts': 1, 'King of Hearts': 10, 'Queen of Hearts': 10, 'Jack of Hearts': 10, '10 of Hearts': 10, '9 of Hearts': 9, '8 of Hearts': 8, '7 of Hearts': 7, '6 of Hearts': 6, '5 of Hearts': 5, '4 of Hearts': 4, '3 of Hearts': 3, '2 of Hearts': 2, 'Ace of Spades': 1, 'King of Spades': 10, 'Queen of Spades': 10, 'Jack of Spades': 10, '10 of Spades': 10, '9 of Spades': 9, '8 of Spades': 8, '7 of Spades': 7, '6 of Spades': 6, '5 of Spades': 5, '4 of Spades': 4, '3 of Spades': 3, '2 of Spades': 2, 'Ace of Clubs': 1, 'King of Clubs': 10, 'Queen of Clubs': 10, 'Jack of Clubs': 10, '10 of Clubs': 10, '9 of Clubs': 9, '8 of Clubs': 8, '7 of Clubs': 7, '6 of Clubs': 6, '5 of Clubs': 5, '4 of Clubs': 4, '3 of Clubs': 3, '2 of Clubs': 2, 'Ace of Diamonds': 1, 'King of Diamonds': 10, 'Queen of Diamonds': 10, 'Jack of Diamonds': 10, '10 of Diamonds': 10, '9 of Diamonds': 9, '8 of Diamonds': 8, '7 of Diamonds': 7, '6 of Diamonds': 6, '5 of Diamonds': 5, '4 of Diamonds': 4, '3 of Diamonds': 3, '2 of Diamonds': 2}

但此时,当我尝试使用此代码打印密钥和值时:

import json

with open('deckfile.json') as f:
    data = [json.loads(line) for line in f]
    for row in data:
        print(row)

我收到错误消息。到目前为止,我已经成功使用了txt和二进制文件。在阅读文件之前,我从未习惯过字典。

我的问题是:太多价值观是什么意思?据我所知,有一本字典:卡片的名称和52张卡片中每张卡片的卡片价值。我使用了一个约定来试图获得密钥和值而没有成功。

1 个答案:

答案 0 :(得分:1)

btn.setText("Roll Die"); btn.setOnAction((ActionEvent event) -> { btn.setDisable(true);//Disable Button Random random = new Random(); int gekozen = Integer.parseInt(tf3.getText()); int numTempValues = random.nextInt(20) + 1 ; int[] tempValues = random.ints(1, 7).limit(numTempValues).toArray(); int finalDieValue = tempValues[numTempValues - 1]; Timeline timeline = new Timeline() ; for (int i = 0 ; i < tempValues.length ; i++) { KeyFrame keyFrame = new KeyFrame(Duration.seconds(0.3).multiply(i+1), actionEvent -> { System.out.println(tempValues[i]); die.setDieFace(tempValues[i]); }); timeline.getKeyFrames().add(keyFrame); } timeline.setOnFinished(actionEvent -> { btn.setDisable(false);//Enable Button System.out.println("You rolled: "+finalDieValue); }); timeline.play(); }); 中,有两个问题:

  1. for key,value in data是词典列表。你可能意味着data
  2. 迭代字典只会迭代其键。要迭代键和值,请执行for key,value in row
  3.   

    我的问题是:太多价值观是什么意思?

    for key,value in row.items()表示for key,value in data应该是一对可迭代的对,可以解压缩到datakey。但是,迭代value时得到的第一件事就是它包含的大词典。为了尝试将字典解压缩到data,Python迭代字典并获取所有52个键,这是太多的值。

    这样想:你不会这样做:

    key, value

    因为该行有许多键和值。你也不会这样做:

    for row in data:
        key, value = row
    

    因为for k in row: key, value = k k的键,它是一个字符串。因此,我们需要row