简体   繁体   中英

Why am I getting a value error for values that use the split() method in python?

I'm trying to separate a list of values into keys and values using a dict. The list if formatted like this:

packagename=version_number
packagename2=version_number2
etc...

Sometimes the version number has special characters, but never an equal sign.

dict = {}
with open('file.rtf') as f:
    for line in f.readlines():
        pkg,ver = line.split('=')
        dict[pkg] = ver

print("%s: %s" % (dict[pkg], dict[ver]))    

When I run the code I get the following error: "ValueError: need more than 1 value to unpack" and I'm not sure why. I've tried modifying the .rtf file by separating the values on either side of the equal sign with spaces, in case that made a difference.

UPDATE

Using a .rtf file was indeed the problem, as pointed out by @tdelaney and others. Code runs smoothly now after converting to plain text. Thanks for your help, guys.

Your input file is in the RTF format, which means it contains formatting codes like this (even if it contains no formatting):

{\rtf1\ansi{\fonttbl\f0\fswiss Helvetica;}\f0\pard
packagename=version_number\par
}

This is not the input your program is expecting, and you're probably getting the error on that first line, which contains no = , and therefore splits to one value, giving your error.

Try converting to txt , or write your code to handle RTF.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM