Reading CSV File as an Input Parameter.
When working in the field of Data Analysis,you will
frequently encounter CSV files. CSV files are a type of text file that stores
tabular data for improved readability, easier understanding, and faster
processing.
This article will provide an introduction to CSV files
and guide you through the various methods of reading and writing them in
Python. Python is our focus because it includes a built-in csv library that
simplifies the process of reading data from CSV files and writing data to them.
The step-by-step Python tutorials in this article are designed to be
straightforward and suitable for beginners.
To read a CSV file as an input parameter in Python,
follow the steps below:
1. Import the necessary libraries: Import the required
libraries, such as csv or pandas, to read and manipulate the CSV file.
2. Process the CSV data: Depending on your needs, you can
process the data in the CSV file. For example, you can iterate over the rows
and access values from specific columns.
3. You have now successfully read the CSV file as an
input parameter and processed the data. From here, you can use the data for
further operations or analysis based on your requirements.
Below is the simple python code.
import pandas as pd
import sys
import csv
def csv_reader_pands(file):
try:
df=pd.read_csv(file)
print(df)
except FileNotFoundError:
print("File not
found")
except Exception as err:
print("An Exception
",err)
if __name__=='__main__':
if len(sys.argv)!=2:
print("Usage: python
program_name.py file")
else:
csv_input_file=sys.argv[1]
csv_reader_pands(csv_input_file)
0 Comments
Thanks for your message.