Write a user defined functions to perform read and write operations onto a student.csv file having fields roll number, name, stream and marks
Answers
Answered by
2
Answer:
CSV (Comma Separated Values) format is one of the most simple and common ways to store tabular data. To represent a CSV file, it must be saved with the .csv file extension.
Let's take an example:
- If you open the above CSV file using a text editor such as sublime text, you will see:
- SN, Name, City
1, Michael, New Jersey
2, Jack, California
- As you can see, the elements of a CSV file are separated by commas. Here, , is a delimiter.
- You can have any single character as your delimiter as per your needs.
- The csv module can also be used for other file extensions (like: .txt) as long as their contents are in proper structure.
Reading CSV files Using csv.reader()
- To read a CSV file in Python, we can use the csv.reader() function.
- Suppose we have a csv file named people.csv in the current directory with the following entries.
Writing CSV files Using csv.writer()
- To write to a CSV file in Python, we can use the csv.writer() function.
- The csv.writer() function returns a writer object that converts the user's data into a delimited string. This string can later be used to write into CSV files using the writerow() function.
Similar questions
Computer Science,
4 months ago
Science,
4 months ago
Business Studies,
4 months ago
Political Science,
9 months ago
Physics,
9 months ago
Physics,
1 year ago
Math,
1 year ago