简体   繁体   English

python3.5中的ValueError

[英]ValueError in python3.5

I am constantly getting this error " ValueError: not enough values to unpack (expected 2, got 1)in for(word,tag) in grp:" This what I have tried # - - coding: utf-8 - - import nltk import itertools import ast import collections import sys import re import time 我不断收到此错误“ ValueError: not enough values to unpack (expected 2, got 1)in for(word,tag) in grp:"这是我尝试过的# --编码:utf-8 --import nltk import itertools导入ast导入集合导入sys导入重新导入时间

f=open('test.txt','r')
text1=f.read()
text2=text1.rstrip()
text3=text2.strip()
#text3=tuple(text1)
#print(text3)

print("text3")


train_data=text3


print(train_data)

f=open('test1.txt','r')
text5=f.read()
#text6=text5.splitlines()
text6=text5.strip()
text7=text6.rstrip()
orig_data=text7

So, train_data is just a str. 因此, train_data只是一个str。 Notice how you read it from a file and don't do anything to change it into code. 请注意,您是如何从文件中读取它的,并且没有做任何事情将其更改为代码。 If you want to confirm, call print(type(train_data)) . 如果要确认,请调用print(type(train_data)) You'll get <class 'str'> . 您将得到<class 'str'>

You can iterate through a str with a for loop, that's why your first loop works, but in your second loop, you're just looping over the characters in the original str. 您可以使用for循环遍历str ,这就是第一个循环起作用的原因,但是在第二个循环中,您只是循环遍历原始str中的字符。

If you want to use it as actual data, you must parse it and turn it into a Python data structure. 如果要将其用作实际数据,则必须对其进行解析并将其转换为Python数据结构。 DO NOT USE EVAL FOR THIS. 请勿为此使用EVAL。 Instead use the ast library (safer and more stable in case of mistakes in your data): 而是使用ast库(在数据出错的情况下更安全,更稳定):

import ast
# … later …
train_data = ast.literal_eval(text3)

Then go on and use train_data as you're using it. 然后继续使用train_data

f=open('C://Users//DELL//Desktop//test.txt','r')
text1=f.read()
text2=text1.rstrip()
text3=text2.strip()
#text3=tuple(text1)
#print(text3)
print("text3")
train_data=text3
print(train_data)

f=open('C://Users//DELL//Desktop//test1.txt','r')
text5=f.read()
#text6=text5.splitlines()
text6=text5.strip()
text7=text6.strip()
orig_data=text7
print(orig_data)

orgword=[]
orgtags=[]
#orig_data=train_data
#print("original data")
#print(orig_data)
for grp in train_data:
    for word in grp:
        orgword.append(word)
        #orgtags.append(tag)

print("Original Words")
print(orgword)
print("Original Tags")
print(orgtags)

#fix it...!!!(U get word in test)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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