Python Read Different Type of Files
In this blog we will read different type of files like text, csv, tsv, json, xml and excel sheet. For that we will use csv, json, xml.etree.ElementTree and xlrd module.
Installation
Installation for reading csv, tsv, json and xml file
csv is part of python’s standard library so you don’t need to install it with pip.
Python comes with a built-in package called json for encoding and decoding JSON data.So no need to install it with pip.
Python’s standard library contains xml package. The xml.etree.ElementTree module implements a simple and efficient API for parsing and creating XML data. so installation is required.
Installation for reading excel sheet
xlrd is a library for reading data and formatting information from Excel files in the historical .xls format.
pip install xlrdSample files
We have created sample files for this blog. If you want to download, click on following links.
sample.txt
sample.csv
sample.json
sample1.json
sample.xml
sample.xlsx
sample.tsv
Read text file
Declare the text file path and open file. After that print.
txtFile = "input/sample.txt"with open(txtFile) as f:
lines = f.read()
print(lines)
pass
Output:
Read each line from text file
with open(txtFile) as f: for line in f:
print (line)
pass
pass
Output:
Read file from readlines()
readlines() — Return all lines in the file, as a list.
with open(txtFile) as f:
lines = f.readlines()
print(lines)
passOutput:
Print each lines
with open(txtFile) as f:
lines = f.readlines()
#print(lines)
for line in lines:
print(line)
pass
passOutput:
Read csv file
import csv module
import csvHelp on built-in function reader in module _csv:
reader(…)
csv_reader = reader(iterable [, dialect=’excel’]
[optional keyword args])
for row in csv_reader:
process(row)
The “iterable” argument can be any object that returns a line
of input for each iteration, such as a file object or a list. The
optional “dialect” parameter is discussed below. The function
also accepts optional keyword arguments which override settings
provided by the dialect.
The returned object is an iterator. Each iteration returns a row
of the CSV file (which can span multiple input lines).
Declare csv file path, open and then read
csvFile = "input/sample.csv"with open(csvFile, newline='') as cf: csv_reader = csv.reader(cf)
print(csv_reader) pass
Output: <_csv.reader object at 0x7f0838395cd0>
Print each line
with open(csvFile, newline='') as cf: csv_reader = csv.reader(cf)
#print(csv_reader) #read each line and print
for each_row in csv_reader:
print(each_row)
pass pass
Output:
Read csv with DictReader
csv.DictReader(f, fieldnames=None, restkey=None, restval=None, dialect=’excel’, args, *kwds)
Create an object that operates like a regular reader but maps the information in each row to a dict whose keys are given by the optional fieldnames parameter.
with open(csvFile, 'r') as cf: reader = csv.DictReader(cf)
print(reader)
pass
Output: <csv.DictReader object at 0x7f0838351290>
with open(csvFile, 'r') as cf: reader = csv.DictReader(cf)
#print(reader) #print each row
for row in reader:
print(row)
pass pass
Output:
with open(csvFile, 'r') as cf: reader = csv.DictReader(cf)
#print(reader) #print each row
for row in reader:
print(f'First name: {row["Firstname"]} and Last name is {row["Lastname"]}')
pass pass
Output:
Read JSON file
Import json module
import jsonOpen json file and read
jsonFile = 'input/sample.json'with open(jsonFile) as jf:
jsonData = json.load(jf)
print(jsonData)
pass
Output:
with open(jsonFile) as jf:
jsonData = json.load(jf)
#print(jsonData)
print('id: ' + str(jsonData['id']))
print('name: ' + str(jsonData['name']))
print('subject: ' + str(jsonData['subject']))
passOutput:
Read another json with multiple records
jsonFile = 'input/sample1.json'with open(jsonFile) as jf1:
jsonData = json.load(jf1)
print(jsonData)
pass
Output:
with open(jsonFile) as jf1:
jsonData = json.load(jf1)
#print(jsonData) for s in jsonData:
print(f'\nid: {s["id"]}')
print(f'name: {s["name"]}')
print(f'subject: {s["subject"]}')
pass pass
Output:
Read XML
import xml.etree.ElementTree as ETParse xml file
xmlFile = 'input/sample.xml'tree = ET.parse(xmlFile)#get the root of xml file
root = tree.getroot()
print(root)
Output: <Element ‘data’ at 0x7f08382efa70>
Get Nutan’s stories in your inbox
Join Medium for free to get updates from this writer.
Print child tag and it’s attributes
tree = ET.parse(xmlFile)#get the root of xml file
root = tree.getroot()
#print(root)#print child tag and it's attributes
for child in root:
print(child.tag)
print(child.attrib)
print("\n")
pass
Output:
Print the children ‘neighbor’ of root node
tree = ET.parse(xmlFile)#get the root of xml file
root = tree.getroot()
#print(root)#print child tag and it's attributes
for child in root:
print(child.tag)
print(child.attrib)
print("\n")
pass#print the children 'neighbor' of root node
for neighbor in root.iter('neighbor'):
print(neighbor.attrib)
print("\n")
pass
Output:
Print the children ‘year’ of root node
tree = ET.parse(xmlFile)#get the root of xml file
root = tree.getroot()
#print(root)#print child tag and it's attributes
for child in root:
print(child.tag)
print(child.attrib)
print("\n")
pass#print the children 'neighbor' of root node
for neighbor in root.iter('neighbor'):
print(neighbor.attrib)
print("\n")
pass#print the children 'year' of root node
for year in root.iter('year'):
print(int(year.text))
print("\n")
pass
Output:
Find all the countries
tree = ET.parse(xmlFile)#get the root of xml file
root = tree.getroot()
#print(root)#print child tag and it's attributes
for child in root:
print(child.tag)
print(child.attrib)
print("\n")
pass#print the children 'neighbor' of root node
for neighbor in root.iter('neighbor'):
print(neighbor.attrib)
print("\n")
pass
#print the children 'year' of root node
for year in root.iter('year'):
print(int(year.text))
print("\n")
pass#find all the countries
for country in root.findall('country'):
rank = int(country.find('rank').text) print("Country ranks are: ")
print(rank)
print(country.attrib)
pass
Output:
Read excel file
import xlrdOpen workbook and take first sheet.
excelFile = "input/sample.xlsx"wb = xlrd.open_workbook(excelFile)sheet = wb.sheet_by_index(0)
print(sheet)
Output: <xlrd.sheet.Sheet object at 0x7f0809e62b10>
Print the numbers of columns in the excel sheet
print(sheet.ncols)Output: 4
Print the column names
for i in range(sheet.ncols):
print(sheet.cell_value(0, i))
passOutput:
Print the number of rows in the excel sheet
print(sheet.nrows)Output: 6
Print the rows values
for i in range(sheet.nrows):
print(sheet.row_values(i))
passOutput:
Print the specific row values
print(sheet.row_values(1))Output:
Read tsv file
import csvOpen tsv file and read
tsvFile = "input/sample.tsv"with open(tsvFile) as tf:
tsv_reader = csv.reader(tf, delimiter="\t") #print each row
for row in tsv_reader:
print(row)
pass
pass
Output:
Read tsv file with DictReader()
with open(tsvFile) as tf: #read tsv file with DictReader()
tsv_reader = csv.DictReader(tf, dialect='excel-tab') #iterate tsv_reader
for row in tsv_reader: #print each row
print(row)
pass
pass
Output:
with open(tsvFile) as tf: #read tsv file with DictReader()
tsv_reader = csv.DictReader(tf, dialect='excel-tab') #iterate tsv_reader
for row in tsv_reader:
#print each row
#print(row) #print row with key
print(row['SrNo'], row['Username'])
pass pass
Output:
That’s it. Thanks for reading.







