CSV stands for Comma Separated Value. This type of file can be used for many applications including databases and spreadsheets. A comma separates each element within the document and it is the comma that allows programs such as MS Excel to put the data in to individual cells, rows and columns. You can view csv files in most text editors and you will see a comma separating each value, as can be seen in the example screenshot from MS Word.
|
|
import csv file = open('activities.csv')#Replace the file name with your file for line in file: print (line)
import csv with open ('activities.csv', 'r') as file: sports = csv.reader(file) for row in sports: print (row[3])
with open ('activities.csv', 'r') as file: sports = csv.reader(file, delimiter = ' ')
import csv level = input("What level do you want to view? ").title() with open ('activities.csv', 'r') as file: sports = csv.reader(file, delimiter = ' ') for row in sports: if row[2] == (level): print (row)
|
import csv activity_ID = [] activity_type = [] activity_level = [] activity_description = [] with open ('activities.csv','r') as file: sports = csv.reader(file) for row in sports: activity_ID.append(row[0]) activity_type.append(row[1]) activity_level.append(row[2]) activity_description.append(row[3]) print (activity_ID) print (activity_type ) print (activity_level) print (activity_description)
|
import csv with open ('activityV3.csv','w+') as file: myFile = csv.writer(file) myFile.writerow(["Id","Activity","Description","Level"]) noOfActivities = int(input("Please enter how many activities you want: ")) for i in range (noOfActivities): activityID = input("Activity " + str(i + 1) + " : Please enter the activity ID: ") activity = input("Activity " + str(i + 1) + " : Please enter the type of activity: ") desc = input("Activity " + str(i + 1) + " : Please enter the description of the activity: ") level = input("Activity " + str(i + 1) + " : Please enter the level of the activity: ") myFile.writerow([activityID, activity, desc,level])
|
#EDITING A CSV FILE #Task 1: Import CSV and create an empty list import csv myList = [] #Task 2: Open the file and populate the empty list with open ('activityv4.csv','r') as file: myFile =csv.reader(file) for row in myFile: myList.append(row) #Task 3: Show the user the content of the file and print row number to make selection easy print ("Please see details of the csv file below:") for i in range (len(myList)): print ("Row " + str(i) + ": " + str(myList[i])) #Task 4: Allow the user to select which row to edit editRow = int(input("\nWhich row would you like to change? Enter 1 - " + str(len(myList)-1) + " :")) print ("Please enter the new details for each of the following :") #Task 5: Allow the user to make add the changes and append changes to the list for i in range (len(myList[0])): newDetails = input("Enter new data for " + str(myList[0][i]) + " :") myList[editRow][i] = newDetails #Task 6: Show the user the new list and confirm changes print ("\nPlease see the details of the new file below:") for i in range (len(myList)): print ("Row " + str(i) + " :" + str(myList[i])) #Task 7: If changes are needed write the new file changeCSV = input ("\nWould you like to make the changes to the csv file ? Y/N").lower() if changeCSV == ("y"): with open ('activityv4.csv','w+') as file: myFile = csv.writer(file) for i in range (len(myList)): myFile.writerow(myList[i])
SUGGESTIONS
We would love to hear from you |
SUBSCRIBE
To enjoy more benefits |