#Declare variables to hold the file name and access mode fileName = "numList.dat" accessMode = "w" #Open the file for writing myFile = open(numList, accessMode) #Write the guest names and ages to the file #The file is a string of numbers myFile.write("This data file contains a list of integers 0-9\n") myFile.write("The numbers are: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,\n") #Close the file myFile.close() #import the csv library which contains functions to help you work with CSV files import csv #Declare variables to hold the file name and access mode fileName = "numList.dat" accessMode = "r" #open the file with open(fileName, accessMode) as myCSVFile: #Read the file contents #specify that we are using a comma to separate the values in the file dataFromFile = csv.reader(myCSVFile, delimiter =",") #Create a for loop that will run once for each row in the list for row in dataFromFile : #print an entire row at a time in a list format print(row) #print the entire row separating the values in the list with a comma print (', '.join(row)) #Print each indivudal value in each row for value in row : print(value + "\n") #Close the file myCSVFile.close()
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