Saturday, December 28, 2024
Google search engine
HomeLanguagesHow to read numbers in CSV files in Python?

How to read numbers in CSV files in Python?

Prerequisites: Reading and Writing data in CSV, Creating CSV filesĀ 

CSV is a Comma-Separated Values file, which allows plain-text data to be saved in a tabular format. These files are stored in our system with a .csv extension. CSV files differ from other spreadsheet file types (like Microsoft Excel) because we can only have a single sheet in a file, and they cannot save cells, columns, or rows. Also, we cannot save formulas in this format.

To parse CSV files in Python, we make use of the csv library. The CSV library contains objects that are used to read, write and process data from and to CSV files. Letā€™s see how we can add numbers into our CSV files using csv library.

Steps to read numbers in a CSV file:

  1. Create a python file (example: gfg.py).
  2. Import the csv library.
  3. Create a nested-list ā€˜marksā€™ which stores the student roll numbers and their marks in maths and python in a tabular format.
  4. Open a new csv file (or an existing csv file) in the ā€˜wā€™ mode of the writer object and other necessary parameters (here delimiter & quoting).
  5. Write into it the list ā€˜marksā€™ with the help of writerows method.
  6. In order to read the rows, make use of reader object and store each row(which is also a list) in a new list ā€˜outputā€™.
  7. Print the list output for verifying the code.

Reading numbers in a CSV file without quotes:

In order to write in our CSV file ā€˜my_csvā€™, Ā we make use of the writerows() method of the writer object. But to read numbers as they are, we will make use of an optional parameter of the writer object, which is ā€˜quotingā€™. The ā€˜quotingā€™ parameter tells the writer which character is to be quoted.

If quoting is set to csv.QUOTE_NONNUMERIC, then .writerow() will quote all fields which contain text data and convert all numeric fields to the float data type.

Code:

Python3




import csv
Ā 
# creating a nested list of roll numbers,
# subjects and marks scored by each roll number
marks = [
Ā Ā Ā Ā ["RollNo", "Maths", "Python"],
Ā Ā Ā Ā [1000, 80, 85],
Ā Ā Ā Ā [2000, 85, 89],
Ā Ā Ā Ā [3000, 82, 90],
Ā Ā Ā Ā [4000, 83, 98],
Ā Ā Ā Ā [5000, 82, 90]
]
Ā 
# using the open method with 'w' mode
# for creating a new csv file 'my_csv' with .csv extension
with open('my_csv.csv', 'w', newline = '') as file:
Ā Ā Ā Ā writer = csv.writer(file, quoting = csv.QUOTE_NONNUMERIC,
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā delimiter = ' ')
Ā Ā Ā Ā writer.writerows(marks)
Ā 
# opening the 'my_csv' file to read its contents
with open('my_csv.csv', newline = '') as file:
Ā Ā Ā 
Ā Ā Ā Ā reader = csv.reader(file, quoting = csv.QUOTE_NONNUMERIC,
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā delimiter = ' ')
Ā Ā Ā Ā Ā 
Ā Ā Ā Ā # storing all the rows in an output list
Ā Ā Ā Ā output = []
Ā Ā Ā Ā for row in reader:
Ā Ā Ā Ā Ā Ā Ā Ā output.append(row[:])
Ā 
for rows in output:
Ā Ā Ā Ā print(rows)


Output:

output of gfg.py

And this is how it looks in the CSV file ā€˜my_csv.csvā€™ which gets created once we run the above code:

my_csv.csv

Reading numbers in a CSV file with quotes:

If quoting is set to csv.QUOTE_ALL then .writerow() will quote all fields and the numbers will now be stored in quotes. To read the numbers from each row, we make use of the reader object from CSV library and store all the rows within a list ā€˜outputā€™, which we would also print afterward.

Code:

Python3




import csv
Ā 
# creating a nested list of roll numbers,
# subjects and marks scored by each roll number
marks = [
Ā Ā Ā Ā ["RollNo", "Maths", "Python"],
Ā Ā Ā Ā [1000, 80, 85],
Ā Ā Ā Ā [2000, 85, 89],
Ā Ā Ā Ā [3000, 82, 90],
Ā Ā Ā Ā [4000, 83, 98],
Ā Ā Ā Ā [5000, 82, 90]
]
Ā 
# using the open method with 'w' mode
# for creating a new csv file 'my_csv' with .csv extension
with open('my_csv.csv', 'w', newline = '') as file:
Ā Ā Ā Ā writer = csv.writer(file, quoting = csv.QUOTE_ALL,
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā delimiter = ' ')
Ā Ā Ā Ā writer.writerows(marks)
Ā 
# opening the 'my_csv' file to read its contents
with open('my_csv.csv', newline = '') as file:
Ā Ā Ā Ā reader = csv.reader(file,
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā quoting = csv.QUOTE_ALL,
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā delimiter = ' ')
Ā Ā Ā Ā Ā 
Ā Ā Ā Ā # storing all the rows in an output list
Ā Ā Ā Ā output = []
Ā Ā Ā Ā for row in reader:
Ā Ā Ā Ā Ā Ā Ā Ā output.append(row[:])
Ā 
for rows in output:
Ā Ā Ā Ā print(rows)


Ā Output:

output of gfg.py

And this how the above input gets stored within ā€˜my_csv.csvā€™ file:

my_csv.csv

RELATED ARTICLES

Most Popular

Recent Comments

ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
źøˆģ²œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
źµ¬ģ›”ė™ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ˜¤ģ‚°ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ•ˆģ–‘ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė™ķƒ„ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ„œģšøģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶„ė‹¹ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ź³”ė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź³ ģ–‘ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ģ„±ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ²œķ˜øė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?