这是我对伪代码的翻译。该代码建议可以将多个列表或数组写入 .dat 文件。我试图尽可能地忠实于伪代码格式,以免在调试过程中迷失方向。为了篇幅,我省略了实际的伪代码。我知道如何将所有内容作为字符串写入 .txt 或 .csv,但是否可以将它们写入同一个文件,允许每个对象保持其原始值?(Num=Num 和 str=str)
#THIS IS THE PREMISE OF THE PSEUDOCODE
#Cooper College maintains a master file of students and credits
#earned. Each semester the master is updated with a transaction
#file that contains credits earned during the semester.
#Each file is sorted in Student ID number order.
这就是我试图翻译成 Python 的内容
#getReady()
# open master "studentFile.dat"
# open trans "semesterCredits.dat"
# open newMaster "updatedStudentFile.dat"
# readMaster()
# readTrans()
# checkBoth()
#return
#
#readMaster()
# input masterID, masterName, masterCredits from master
# if eof then
# masterID = HIGH_VALUE
# endif
#return
这就是我试图用来创建起始文件的内容
#EACH LIST HAS A RANGE OF [0, 4]
masterID =[100000,100001,100002,100003]
masterName =['Bevis, Ted','Finch, Harold','Einstein, Al','Queen, Liz']
masterCredits = [56,15,112,37]
master = open('studentFile.dat','wb')
master.write(masterID,masterName,masterCredits)
print master.readlines()
#THIS IS MY TRACEBACK ERROR
#Traceback (most recent call last):
# File "C:/Users/School/Desktop/Find the Bugs Ch7/debug07-03start.py",
#line 6, in <module>
#master.write(masterID,masterName,masterCredits)
#TypeError: function takes exactly 1 argument (3 given)
预期输出
print master.readlines()
[[100001,'Bevis, Ted',56],[100002,'Finch, Harold',15],[100003,'Einstein, Al',112]...]