diff --git a/CSV_files/README.md b/CSV_files/README.md new file mode 100644 index 00000000..0f92842b --- /dev/null +++ b/CSV_files/README.md @@ -0,0 +1,10 @@ +# Reading and Writing a CSV File +There are various ways to read a CSV file that uses either the CSV module or the pandas library. + +- csv Module: The CSV module is one of the modules in Python which provides classes for reading and writing tabular information in CSV file format. + +- pandas Library: The pandas library is one of the open-source Python libraries that provide high-performance, convenient data structures and data analysis tools and techniques for Python programming. + +* **Read using csv.DictReader() class:** the CSV file is first opened using the open() method then it is read by using the DictReader class of csv module which works like a regular reader but maps the information in the CSV file into a dictionary. The very first line of the file consists of dictionary keys. + + diff --git a/CSV_files/assets/addresses.csv b/CSV_files/assets/addresses.csv new file mode 100644 index 00000000..e7bba0d3 --- /dev/null +++ b/CSV_files/assets/addresses.csv @@ -0,0 +1,6 @@ +John,Doe,120 jefferson st.,Riverside, NJ, 08075 +Jack,McGinnis,220 hobo Av.,Phila, PA,09119 +"John ""Da Man""",Repici,120 Jefferson St.,Riverside, NJ,08075 +Stephen,Tyler,"7452 Terrace ""At the Plaza"" road",SomeTown,SD, 91234 +,Blankman,,SomeTown, SD, 00298 +"Joan ""the bone"", Anne",Jet,"9th, at Terrace plc",Desert City,CO,00123 diff --git a/CSV_files/read_csv.py b/CSV_files/read_csv.py new file mode 100644 index 00000000..2a5712c8 --- /dev/null +++ b/CSV_files/read_csv.py @@ -0,0 +1,17 @@ +import csv + +def read_using_DictReader(): + # opening the CSV file + with open('CSV_files/assets/addresses.csv', mode ='r') as file: + # reading the CSV file + csvFile = csv.DictReader(file) + + # displaying the contents of the CSV file + for lines in csvFile: + return lines + + + + +if __name__=="__main__": + print(read_using_DictReader()) \ No newline at end of file