Python分割函数。解包错误的值太多

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21254645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 22:24:50  来源:igfitidea点击:

Python split function. Too many values to unpack error

pythonstring

提问by user3156971

I have a python function that must read data from file and split it into two key and value, and then store it in dictionary. Example: file:

我有一个 python 函数,它必须从文件中读取数据并将其拆分为两个键和值,然后将其存储在字典中。示例:文件:

http://google.com 2
http://python.org 3
# and so on a lot of data

I use the split function for it, but when there is really a lot of data it raises value error

我为它使用了 split 函数,但是当真的有很多数据时,它会引发值错误

ValueError: too many values to unpack

What can I do about this ?

我该怎么办?

This is the exact code that fails

这是失败的确切代码

with open(urls_file_path, "r") as f:
    for line in f.readlines():
        url, count = line.split()# fails here
        url_dict[url] = int(count)

采纳答案by thefourtheye

You are trying to unwrap the split list in to these two variables.

您正在尝试将拆分列表解包到这两个变量中。

url, count = line.split()

What if there is no space or two or more spaces? Where will the rest of the words go?

如果没有空格或两个或更多空格怎么办?剩下的单词会去哪里?

data = "abcd"
print data.split()    # ['abcd']
data = "ab cd"
print data.split()    # ['ab', 'cd']
data = "a b c d"
print data.split()    # ['a', 'b', 'c', 'd']

You can actually check the length before assigning

您实际上可以在分配之前检查长度

with open(urls_file_path, "r") as f:
    for idx, line in enumerate(f, 1):
        split_list = line.split()
        if len(split_list) != 2:
            raise ValueError("Line {}: '{}' has {} spaces, expected 1"
                .format(idx, line.rstrip(), len(split_list) - 1))
        else:
            url, count = split_list
            print url, count

With the input file,

使用输入文件,

http://google.com 2
http://python.org 3
http://python.org 4 Welcome
http://python.org 5

This program produces,

这个程序产生,

$ python Test.py
Read Data: http://google.com 2
Read Data: http://python.org 3
Traceback (most recent call last):
  File "Test.py", line 6, in <module>
    .format(idx, line.rstrip(), len(split_list) - 1))
ValueError: Line 3: 'http://python.org 4 Welcome' has 2 spaces, expected 1

Following @abarnert's comment, you can use partitionfunction like this

按照@abernert 的评论,您可以使用这样的partition功能

url, _, count = data.partition(" ")

If there are more than one spaces/no space, then countwill hold rest of the string or empty string, respectively.

如果有多个空格/没有空格,count则将分别保存字符串的其余部分或空字符串。

If you are using Python 3.x,you can do something like this

如果您使用的是 Python 3.x,则可以执行以下操作

first, second, *rest = data.split()

First two values will be assigned in firstand secondrespectively and the rest of the list will be assigned to rest, in Python 3.x

在 Python 3.x 中,前两个值将分别分配给firstsecond,列表的其余部分将分配给rest,