def main():
#Open the files we will be using
inFile = open("names.dat", 'r')
outFile = open("StudentList.csv", 'w')
#Process each line of the input file and output to the CSV file
for line in inFile:
data = line.split()
first = data[0]
last = data[1]
idNum = data[3]
major = data[6]
year = data[5]
if data[5] == "Freshman":
print("FR")
elif data[5] == "Sophomore":
print("SO")
elif data[5] == "Junior":
print("JR")
elif data[5] == "Senior":
print("SR")
Und das ist die aktuelle Ausgabe
Dougherty,Antwan,ADougherty556,Philosophy,Freshman
Knox,Patrick,PKnoxX539,Art,Senior
Parrish,Shane,SParrish474,Gerontology,Sophomore
Kerr,Jerome,JKerrX130,Journalism,Senior
Foley,Adriel,AFoley752,Aviation,Junior
Wagner,Seamus,SWagner837,Electrical,Senior
Hobbs,Ashton,AHobbs844,Urban,Freshman
Petty,Evie,EPetty205,Public,Sophomore
Myers,Leandro,LMyers183,Counseling,Senior
Roberts,Julia,JRoberts830,Library,Freshman
Nun stellt sich die Frage, wie ich den korrekten Wert von „FR" anstelle von Freshman in die CSV-Datei einfüge?
Lösung des Problems
Wenn dies Python ist, könnte die Funktion main() zum Schreiben von Elementen in die outFile so aussehen.
def main():
#Open the files we will be using
inFile = open("names.dat", 'r')
outFile = open("StudentList.csv", 'w')
#Process each line of the input file and output to the CSV file
for line in inFile:
data = line.split()
first = data[0]
last = data[1]
idNum = data[2]
major = data[3]
year = data[4]
# You're indexing seems to be off? You have no input example, so I will
# assume it's like the output.
# I suppose you want all the other fields in the same order, just switch Freshman to FR
outFile.write("{0},{1},{2},{3},".format(first,last,idNum,major))
if year == "Freshman":
outFile.write("FR")
elif year == "Sophomore":
outFile.write("SO")
elif year == "Junior":
outFile.write("JR")
elif year == "Senior":
outFile.write("SR")
outFile.write("\n")
Keine Kommentare:
Kommentar veröffentlichen