Python ValueError:没有足够的值来解包(预期值为8,实际值为1)

Python ValueError:没有足够的值来解包(预期值为8,实际值为1),python,Python,我的分割函数有什么问题吗 导入系统 导入操作系统 进口稀土 filename=“data.txt” mydata=[] 打开(文件名为“r”)作为f: 印刷品(f) 对于f中的i: 直线=0 行+=1 记录编号、大小、用户名、事务、字段类型、数字值、字符值、日期值=i.split(“|”) 行到打印=生成行到打印(确定=真) 如果字段类型='N': 如果字符_值!=“”或日期值!=''或数值_值=='': 行到打印=生成行到打印(确定=假) elif字段类型=='C': 如果是数字值!=''或日

我的分割函数有什么问题吗

导入系统 导入操作系统 进口稀土 filename=“data.txt” mydata=[] 打开(文件名为“r”)作为f: 印刷品(f) 对于f中的i: 直线=0 行+=1 记录编号、大小、用户名、事务、字段类型、数字值、字符值、日期值=i.split(“|”) 行到打印=生成行到打印(确定=真) 如果字段类型='N': 如果字符_值!=“”或日期值!=''或数值_值=='': 行到打印=生成行到打印(确定=假) elif字段类型=='C': 如果是数字值!=''或日期值!=''或字符_值=='': 行到打印=生成行到打印(确定=假) elif字段类型=='D': 如果是数字值!=''或字符_值!=''或日期_值=='': 行到打印=生成行到打印(确定=假) def MakelineTopPrint(正常): 如果确定: 行到打印=“{}{}{}确定---{}{124}{}{124}{}{}{124}{}{}}{124}”。格式(行、记录编号、大小、用户名、事务、字段类型、数值、字符值、日期值) 其他: 行到打印=“{}{}{}否---{}{}{}{}{}{}}{}}{}}{}}}{}格式(行、大小、记录编号、事务、字段类型、数值、字符值、日期值) 返回行\u到\u打印 打印(行到行打印)
您需要使用open()在
缩进中移动for循环,如下所示:

with open(filename,"r") as f:
    for i in f:
       do_stuff()

我已经修复了缩进错误,并在导入之后立即移动了函数定义

import sys
import os
import re

def makeLineToPrint(ok):
    if ok:
        line_to_print = "{}{} OK--- {}|{}|{}|{}|{}|{}|{}".format(line, record_number, size, username, transaction, field_type, numeric_value, character_value, date_value)
    else:
        line_to_print = "{}{} NO--- {}|{}|{}|{}|{}|{}".format(line, size, record_number, transaction, field_type, numeric_value, character_value, date_value)
    return line_to_print

filename= "data.txt"
mydata= []
with open(filename, "r") as f:
    print(next(f))
    line = 0
    for i in f:
        line += 1

        record_number, size, username, transaction, field_type, numeric_value, character_value, date_value = i.split('|')

        line_to_print = makeLineToPrint(ok = True)    

        if field_type == 'N':
            if character_value != ' ' or date_value != ' ' or numeric_value == ' ':
                        line_to_print = makeLineToPrint(ok = False)        
        elif field_type == 'C':
                if numeric_value != ' ' or date_value != ' ' or character_value == ' ':
                        line_to_print = makeLineToPrint(ok = False)        

        elif field_type == 'D':
                if numeric_value != ' ' or character_value != ' ' or date_value == ' ':
                        line_to_print = makeLineToPrint(ok = False)
        print(line_to_print)


我明白你在说什么。那么我是否需要移动makelinetoprint函数?是的,函数定义应该在导入后立即出现。请使用新代码和看到的错误更新您的问题。我还建议在您的
之后添加
print(f)
,并将open()作为f:
语句,以验证您是否成功打开了文件。您好,您知道这意味着什么吗?“ValueError:没有足够的值来解包(预期为8,得到1)”您试图i.split(“|”)的行没有任何“|”。我假设data.txt的第一行是一个标题?如果是这样,您可以在for循环中添加一个检查
如果行==0:
行+=1
继续
,或者在
下一步(f)