在 Python 中拆分文件中的数据
Splitting data in file in Python
class Student:
def __init__(self, name, hours, qpoints):
self.name = name
self.hours = float(hours)
self.qpoints = float(qpoints)
def getName(self):
return self.name
def getHours(self):
return self.hours
def getQPoints(self):
return self.qpoints
def gpa(self):
return self.qpoints/self.hours
def makeStudent(infoStr):
name, hours, qpoints = infoStr.split("\t")
return Student(name, hours, qpoints)
def main():
fileName = input("Enter file name: ")
infile = open(fileName, "r")
best = makeStudent(infile.readline())
for line in infile:
s = makeStudent(line)
if s.gpa() > best.gpa():
best = s
infile.close()
print("The best student is:", best.getName())
print("hours:", best.getHours())
print("GPA:", best.gpa())
if __name__ == '__main__':
main()
我想从文本文件中读取行,用“\t”或“,”将其拆分,这样我就可以将它分配给变量,我得到 "ValueError: not enough values to unpack (expected 3, got 1) in makeStudent(infoStr) function. File I use is written correctly, I get same error if I edit file and code to ",”而不是“\t” “。为什么会这样?编辑:问题出在文本中跳过行。已解决。
有时 infoStr
行可能不包含您要拆分的字符(例如空行 ''
)。将其包装在一个 try 块中,你应该没问题。
try:
name, hours, qpoints = infoStr.split('\t')
except ValueError:
name, hours, qpoints = None, None, None
然后您需要在实例化 Student
之前处理 None
情况。
请注意,您已经在 for line in infile
开始的块中迭代文件行,因此无需在 infile.readline()
内执行。
您还可以在将行格式发送到您的函数之前检查行格式(或检查函数中的格式,无论您喜欢什么)。
{truncated code}
# This loop will put on each iteration the next line of the file in the "line" var.
for line in infile:
# You need two commas in your line to be able to split it in 3 values.
if line.count(",") != 2:
print("WARN: Invalid format in line: "+line)
# Of course that you could implement some counter to identify
# the problematic line location within the file...
quit()
s = makeStudent(line)
if s.gpa() > best.gpa():
best = s
{truncated code}
我敢打赌这是一个经典的制表符与 spaces 问题。由于 IDE 格式或搜索和替换失控,您的文件实际上可能 space 分开。
试试这个:
def makeStudent(infoStr):
FAKE_TAB = ' '
name, hours, qpoints = infoStr.split(FAKE_TAB)
return Student(name, hours, qpoints)
如果这不起作用,请手动确定每行中每个值之间有多少 space,然后用它替换 FAKE_TAB。不可否认,这是一个略显粗略的补丁......
class Student:
def __init__(self, name, hours, qpoints):
self.name = name
self.hours = float(hours)
self.qpoints = float(qpoints)
def getName(self):
return self.name
def getHours(self):
return self.hours
def getQPoints(self):
return self.qpoints
def gpa(self):
return self.qpoints/self.hours
def makeStudent(infoStr):
name, hours, qpoints = infoStr.split("\t")
return Student(name, hours, qpoints)
def main():
fileName = input("Enter file name: ")
infile = open(fileName, "r")
best = makeStudent(infile.readline())
for line in infile:
s = makeStudent(line)
if s.gpa() > best.gpa():
best = s
infile.close()
print("The best student is:", best.getName())
print("hours:", best.getHours())
print("GPA:", best.gpa())
if __name__ == '__main__':
main()
我想从文本文件中读取行,用“\t”或“,”将其拆分,这样我就可以将它分配给变量,我得到 "ValueError: not enough values to unpack (expected 3, got 1) in makeStudent(infoStr) function. File I use is written correctly, I get same error if I edit file and code to ",”而不是“\t” “。为什么会这样?编辑:问题出在文本中跳过行。已解决。
有时 infoStr
行可能不包含您要拆分的字符(例如空行 ''
)。将其包装在一个 try 块中,你应该没问题。
try:
name, hours, qpoints = infoStr.split('\t')
except ValueError:
name, hours, qpoints = None, None, None
然后您需要在实例化 Student
之前处理 None
情况。
请注意,您已经在 for line in infile
开始的块中迭代文件行,因此无需在 infile.readline()
内执行。
您还可以在将行格式发送到您的函数之前检查行格式(或检查函数中的格式,无论您喜欢什么)。
{truncated code}
# This loop will put on each iteration the next line of the file in the "line" var.
for line in infile:
# You need two commas in your line to be able to split it in 3 values.
if line.count(",") != 2:
print("WARN: Invalid format in line: "+line)
# Of course that you could implement some counter to identify
# the problematic line location within the file...
quit()
s = makeStudent(line)
if s.gpa() > best.gpa():
best = s
{truncated code}
我敢打赌这是一个经典的制表符与 spaces 问题。由于 IDE 格式或搜索和替换失控,您的文件实际上可能 space 分开。
试试这个:
def makeStudent(infoStr):
FAKE_TAB = ' '
name, hours, qpoints = infoStr.split(FAKE_TAB)
return Student(name, hours, qpoints)
如果这不起作用,请手动确定每行中每个值之间有多少 space,然后用它替换 FAKE_TAB。不可否认,这是一个略显粗略的补丁......