# Item Counter # by Robert Layton import csv # create our file with open("names.dat",'wb') as csvfile: csvwriter = csv.writer(csvfile) # write names to file csvwriter.writerow(['George Washington']) csvwriter.writerow(['John Adams']) csvwriter.writerow(['Thomas Jefferson']) csvwriter.writerow(['James Madison']) csvwriter.writerow(['James Monroe']) csvwriter.writerow(['John Quincy Adams']) csvwriter.writerow(['Andrew Jackson']) # close the file csvfile.close() #Read the file and show how many people are in it numNames = 0 #Create names list to store our names namesList = [] namesFile = open("names.dat",'r') #open with csv reader csvNamesFile = csv.reader(namesFile) #iterate through the csv file lines for i in csvNamesFile: print(str(i)) numNames += 1 namesFile.close() print '\nNumber of names:',numNames
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question