BIG DATA INSIGHTS- PART 3
In last two tutorials we discussed on how to create json and xml file(s). Then, on how to convert them from format to another using online converters. In this tutorial we will be discussing on,
How to undergo reading and writing into .csv file using Python.
How to perform reading and writing process in .txt file using Python.
How to read and write in .xml file using Python.
A Comma Separated Values (CSV) file is a plain text file that contains a list of data. CSV files are mostly used for importing and exporting customer or order data, to and from your database. HTML was designed to display the data — with focus on how data looks. XML was designed to carry data — with focus on what data is. In this tutorial, we are going to see
1.Reading and Writing into CSV file using Python:
Python comes with csv packages that supports in reading and writing into comma separated value file. Open a notepad file..type the content by comma separated values. Save the file with the extension as check.csv as shown in Fig 1.and then read the file using reader function in Google Colab. Select the check.csv file and then click open button. Now , type the code given below in colab to find the file gets reading as shown in Fig 2.
import csv
# opening the CSV file
with open(‘check.csv’, mode =’r’)as file:
# reading the CSV file
csvFile = csv.reader(file)
# displaying the contents of the CSV file
for lines in csvFile:
print(lines)
Try this code for writing into .csv file: The writer method will help to create a write object and then using writerow method it is possible to write row by row into the csv file. To write into csv file import csv and follow the below code and shown in Fig 3.
import csv
# field names
fields = [‘Name’, ‘Branch’, ‘Year’, ‘CGPA’]
# data rows of csv file
rows = [ [‘Nikhil’, ‘MECH’, ‘2’, ‘9.0’],
[‘Sanchit’, ‘EEE’, ‘2’, ‘9.1’],
[‘Aditya’, ‘IT’, ‘2’, ‘9.3’],
[‘Sagar’, ‘ECE’, ‘1’, ‘9.5’],
[‘Prateek’, ‘CSE’, ‘3’, ‘7.8’],
[‘Sahil’, ‘AUTO’, ‘2’, ‘9.1’]]
# name of csv file
filename = “university_records.csv”
# writing to csv file
with open(filename, ‘w’) as csvfile:
# creating a csv writer object
csvwriter = csv.writer(csvfile)
# writing the fields
csvwriter.writerow(fields)
# writing the data rows
csvwriter.writerows(rows)
You can find a new file named “university_records.csv” file as shown in Fig 4. gets created and the set of rows defined gets added into csv file.
2.Reading and writing into .txt file:
A file which is saved with the extension as .txt act as text file. So, now try this code in python IDE environment to write into file program.txt file as “Give me your blood, and I shall give you freedom” and close it. Again open the same file in read mode and read line by line using readline method and split them into word by word using split method as shown in Fig 5.
f=open(‘file program.txt’,’w’)
f.writelines(“Give me your blood, and I shall give you freedom”)
f.close()
list1=list()
f=open(‘file program’,’r’)
for i in f.readline().split():
if(i[0].casefold()==’a’ or i[0].casefold()==’e’ or i[0].casefold()==’i’ or i[0].casefold()==’o’ or i[0].casefold()==’u’):
with open(‘vowels.txt’,’a’) as f2:
f2.write(i+” “)
else:
with open(‘consonants.txt’,’a’) as f3:
f3.write(i+” “)
Now fetch first letter of each word by its index and compare it vowels and it true, then write those words into vowels.txt and if not then write those words into consonants file consonants.txt as shown in Fig 6.
3. Reading and Writing into xml file:
First will see how to read from xml file. Create an xml file as shown below:
Now, open python IDE environment and type the code as shown below for reading the data items from this xml file. We always need a parser to parse each and every tag defined in xml file. DOM — Document Object Model is needed to be imported from xml package. Using parse method the user-defined tags can be parsed .
from xml.dom import minidom
# parse an xml file by name
mydoc = minidom.parse(‘items.xml’)
items = mydoc.getElementsByTagName(‘item’)
# one specific item attribute
print(‘Item #2 attribute:’)
print(items[1].attributes[‘name’].value)
# all item attributes
print(‘\nAll attributes:’)
for elem in items:
print(elem.attributes[‘name’].value)
# one specific item’s data
print(‘\nItem #2 data:’)
print(items[1].firstChild.data)
print(items[1].childNodes[0].data)# all items data
print(‘\nAll item data:’)
for elem in items:
print(elem.firstChild.data)
To create an xml file: Import an xml package and use Element and SubElement method to create root node and type some values into them and save that file as filename.xml as shown in Fig 10. Type the code as shown below for creating an xml file as shown in Fig 9.
Now you can find the filename.xml file created in your python installed path.
Thanks for reading this article..In next tutorial will discuss on xml and json parser(s) used in Android Programming.