Author name: cndro

Blogs

How to Work with JSON Data in Python

JSON stands for JavaScript Object Notation which was derived from the subdivision of Javascript Programming Language dealing with Object Literal Syntax. It is used for representing structured data and is very effective for transmitting and receiving data between a server and web application. JSON is a built-in package that came with Python. This package is very good for encoding and decoding JSON data. Now, we’ll learn about 2 different terms in JSON, we have Serialization and Deserialization. Serialization can be referred to as a way of encoding JSON, and how we use the dump() method for writing data to files. Deserialization on the other hand means the conversion of JSON objects into their respective Python objects where we use the load() and loads() method to change the JSON encoded data into Python Objects. We will go further to carry out some implementation below; How to Convert JSON TO Dictionary We will look into how we can convert our JSON data into a dictionary format by using the JSON module and the JSON.loads() method. An example of how to convert a JSON to a dictionary is shown below; 1 #import json module 2import json 3 4#our json string 5user = ‘{“member”: “registered”, “status”: [“paid”, “active”]}’ 6#we use the .loads() method to convert our data 7user_dict = json.loads(user) 8#here we print the dictionary 9print(user_dict) 10# result: {“member”: “registered”, “status”: [“paid”, “active”]} 11 12print(user_dict[‘status’]) 13# Output: [“paid”, “active”] How to Convert Dictionary to JSON We can do a vice-versa of converting dictionary to JSON format as well. To convert a dictionary to JSON format, we will use the JSON module .dumps() method to handle this as shown below: 1#import json module 2import json 3#our dictionary 4user_dict = {‘name’: ‘Todd’, 5’age’: 28, 6’married’: None 7} 8#use the dumps method to change the dictionary back to json 9user_json = json.dumps(user_dict) 10#print our result 11print(user_json) 12# Output: {“name”: “Todd”, “age”: 28, “married”: null} 13   How to Read a JSON File We all must have had a struggle reading a JSON file that possibly might not return what we desired. Now, we’ll demonstrate an example of reading a JSON file in Python by using the .load() method. How to Write JSON to a file What we need to do is finally test how we can write out our JSON data into any file. We’ll demonstrate that by writing JSON data to a text format with the example code below. 1#import json module 2import json 3#the user details 4user_dict = {‘name’: ‘Todd’, 5’age’: 28, 6’married’: None 7} 8#we open an empty user.txt file here before we begin to dump the data 9with open(‘user.txt’, ‘w’) as json_file: 10 json.dump(user_dict, json_file) Pretty Print Our JSON Data We can also pretty print our JSON data to give it an appropriate structure, indenting, whitespace, and makes it look neater than the original form. An example of how we can implement this is shown below: 1import json 2 3user_data = ‘{“member”: “registered”, “status”: [“paid”, “active”]}’ 4 5# Getting dictionary 6user_dict = json.loads(user_data) 7 8# Pretty Print the user dict data 9print(json.dumps(user_dict, indent = 4, sort_keys=True))   If you found this post helpful, let us know in the comments box.

Blogs

How to Connect Python with SQL Database

In this tutorial, we will learn how to connect Python with SQL Database. Python is a programming language that allows users to work quickly and integrate systems more effectively while MySQL is the most popular Open Source Relational SQL database management system used for developing web-based software applications. We need to know that Python can be used in database applications and it is also very efficient. Let’s jump right in. How to Get Started We need to have MySQL installed on our computer before we dive in. You can download MySQL database at their Official WebsiteMySQL :: MySQL Downloads here. We also need to create a connection between MySQL and Python, the mysql.connector module can be used to handle this. To install mysql.connector, use the Pip module to install it by running the command below; pip install mysql-connector-python Connect to Database Now, we need to provide our credentials and connect to the database by using the code below. 1# import the connector here 2import mysql.connector 3 4# initiate a connector object 5the_db = mysql.connector.connect( 6 host = “localhost”, 7 user = “yourusername”, 8 password = “yourpassword” 9) 10 11# Print the object 12print(the_db)   Create Database Here, we will create our database and further create tables, the cursor instance will be parsed in with the CREATE command in creating our database. 1#import the connector module 2import mysql.connector 3 4#create a connection 5the_db = mysql.connector.connect( 6 host = “localhost”, 7 user = “yourusername”, 8 password = “your_password” 9) 10 11#we then create an instance of cursor class to use in executing our CREATE command 12cursor = the_db.cursor() 13 14#we then use cursor exceute with CREATE DATABASE command to create database CNDRO 15cursor.execute(“CREATE DATABASE cndro”)   Create Tables We can create tables by specifying our columns and as well the data types pertaining to them before enclosing them with the Cursor Execute Command. 1#import the connector here 2import mysql.connector 3#we provide our connection object using our credentials which now include the database name 4 5mydb = mysql.connector.connect( 6 host = “localhost”, 7 user = “yourusername”, 8 password = “your_password”, 9 database = “cndro” 10) 11#the cursor instance was created here 12cursor = mydb.cursor() 13 14#we pass the cursor to create the table named Students 15cursor.execute(“CREATE TABLE students (Lastname VARCHAR(255), Firstname VARCHAR(255), Address VARCHAR(255) )”)   If you found this post helpful, let us know in the comments box.

Uncategorized

How to Calculate Week Numbers For Each Quarter in Tableau

In this tutorial, we will demonstrate how we can generate week numbers for different quarters of the year in Tableau. Before we move to that, we need to understand some important date functions we will use in our calculation and their uses. With the help of the date function, we can manipulate dates in our data source. DATEADD It helps to generate the specific date with the specified number interval added to the specified date_part of that date. An example is what we have below; DATEADD(‘month’, 3, #2004-04-15#) = 2004-07-15 12:00:00 AM DATEPART The DATEPART function can be used to return part of a date. The tableau DATEPART function also accepts YEAR, MONTH, DAY. DATEPART(‘year’, #2004-04-15#) = 2004 DATEPART(‘month’, #2004-04-15#) = 4 DATETRUNC The Tableau DATETRUNC is used to return the first day of the specified date part. We are to use the first argument to specify the date part which then returns a new date. This DATETRUNC usually accepts YEAR, MONTH, DAY, etc. DATETRUNC(‘quarter’, #2004-08-15#) = 2004-07-01 12:00:00 AM DATETRUNC(‘month’, #2004-04-15#) = 2004-04-01 12:00:00 AM How to Calculate Week Numbers by Quarter We are going to demonstrate this with the Superstore dataset. First, we are to drag our order Date to the Row Pane and select the date as YEAR (this should have been selected automatically). After then, we will also drag the date again to the pane and select it as QUARTER. Now, we will place in the Order Date again on the Pane but now we will select it as a Week Number Now, we have our week number on the pane. We can as well do a table calculation to get the Week Number and drop it on the pane by using the DATEPART function. We will use the table calculation below and change the field from continuous to Discrete. MIN(DATEPART(‘week’, [Order Date])) After this we need to calculate the weeks by quarter. The DATEPART and DATETRUNC function will be used to calculate this. The formula used is this; MIN(1+(DATEPART(‘week’,[Order Date])-DATEPART(‘week’,DATETRUNC(‘quarter’,[Order Date])))) Just like what we did earlier, we will create a calculated field and use the formula above. After this, drag the field to the pane and change the field from continuous to Discrete, which then generates each week’s number by quarter. Thanks for reading this post. If you have any questions or observations, use the comment box below.

Blogs

How to Create an EC2 Instance In AWS -A Step by Step Guide

How to Create an EC2 Instance In AWS -A Step by Step Guide In this tutorial, we’ll be demonstrating how we can create an EC2 Instance in AWS. Let’s discuss a little about what Amazon EC2 is. The Amazon EC2 comprises both Windows and Linux-based operating systems which allows businesses owners to run application programs in the Amazon Web Services (AWS) public cloud. With EC2, we can launch as many virtual servers as we want and this provides compute capacity for IT projects and cloud workloads. How to set up Amazon EC2 Instance Sign in The first thing we are to do is to sign in to our AWS Management Console. We will then search for EC2 on the search bar which is under services. Create Instance Now, we’ll see the EC2 dashboard as seen in the image above. Afterward, we will click on Instances, which is by the left and this opens a new page as seen below. We will then click on the launch instances drop-down and select Launch Instances. Choose AMI On selecting the Launch Instances, it opens up a new page, which is for selecting our desired Amazon Machine Image(AMI). We’ll select the Ubuntu Server in this tutorial. So, you click the select button whenever you locate the Ubuntu Server. Choose Instance Type What we need to do next is to select our instance type. What we will do here is to select the Free tier eligible option as displayed in the image below. Then, after selecting this option, we will click on the Next: Configure Instance Details button to move to the next page. Configure Instance Now, it opens up the Configure Instance Page. Here, we won’t make any changes to the default settings we see there, we will then move to the next page by clicking on Next: Add Storage button. Add Storage What we need to do here is to add storage. You can go with the default settings here or change it to your desired size. Then we select the Next: Add Tags Button. Add Tags Here, we’ll be adding a tag that helps us to manage our instances and images. Click on the add tag and provide a key and value inside each box. We can use Demokey as the key and as well supply mypass under the value respectively. We then click on Next: Configure Security Group button. Configure Security Groups What we need to do next before we finish the whole process is to configure the security group. To do this we provide rules as SSH and HTTP. We can also provide more rules as we want. We do that by clicking the Add Rule button. We’ll also be changing our HTTP source from custom to Anywhere, so our application can be accessible to all users. After, click on Review and Launch Button. Review the whole Process Now, we will review all the settings and configurations we’ve made so far before we then click on the Launch Button. After selecting the Launch button, an image pops up which asks us how we want to generate our Key-Pair. We will select create a new key pair option here and use the RSA format and also type in the key name as mykey and we then click on Download Key Pair Button. After that, we scroll down and click on Launch Instances Button Now, our Instance will be up and running!! Click on EC2 Dashboard to view your running Instance.

Blogs

How to Extract Text from Images in Python Using Pytesseract OCR

In this tutorial, we’ll show you how to convert text from images into a machine-readable format with the help of the Python Pytesseract module. The Pytesseract Module is a Python wrapper for the Google Tesseract library for OCR. We will be using this module to convert the words in an image to a string. Optical Character Recognition(OCR) has been seen as a field of research in pattern recognition, artificial intelligence, and computer vision. This technique of extracting text from images is generally carried out by data scientists, software engineers, and at different work environments, whereby we know it’s certain the image would contain text data. Installation To install the Pytesseract on our machine, we will need to download the package. In this tutorial, we will use the Windows Operating system. You can as well download it like this: Pytesseract pip install Pytesseract Pillow pip install pillow The library requires the tesseract.exe binary to be indicated when specifying the path. So, during our installation, we can copy the path and keep it for use in the code later. This path highlighted in the image will be used in our code.   Sample one We will convert this particular image below to text by using the pytesseract module: Code: #we first import our libraries here from PIL import Image from pytesseract import * #Here we specified the path to our tessseract installation pytesseract.tesseract_cmd = “C:\\Users\\CNDRO\\AppData\\Local\\Programs\\Tesseract-OCR\\tesseract.exe” #This is the name of the image we have above image_path = “brush.png” # Opening the image & storing it in an image object img = Image.open(image_path) #Providing the location to pytesseract library #pytesseract.tesseract_cmd = pytesseract # we will use this particular function to extract the text from the image text = pytesseract.image_to_string(img) # We will display the result below print(text[:-1])   Output: Sample Two Let’s say we have an image that has a lot of text, we can as well use the pytesseract module to extract our text from the image. We will demonstrate it with the image below: Code: #we first import our libraries here from PIL import Image from pytesseract import * #Here we specified the path to our tessseract installation pytesseract.tesseract_cmd = “C:\\Users\\CNDRO\\AppData\\Local\\Programs\\Tesseract-OCR\\tesseract.exe” #This is the name of the image we have above image_path = “behind.png” # Opening the image & storing it in an image object img = Image.open(image_path) #Providing the location to pytesseract library #pytesseract.tesseract_cmd = pytesseract# we will use this particular function to extract the text from the image text = pytesseract.image_to_string(img) # We will display the result below print(text[:-1]) Output: Thanks for reading this post. If you found this post helpful, share, and follow us for more tutorial posts.

Blogs

Introduction to Paginated Reports in Power BI

  Paginated reports are defined as highly formatted, pixel-perfect output result, reformed for printing or used for PDF generation. They are called “paginated” because they are designed to suit well on multiple pages and as well display all the data in a table, even if the table spans multiple pages. This Paginated reports are also perfect for operational reports, like sales invoices, report cards e.t.c. This article will introduce you to Paginated reports in Power BI. Let’s jump right in. Requirements A user needs a Power BI Pro license to publish a report to the service. You need to create a workspace in Power BI services and assign a dedicated capacity for paginated reports. In a Premium Gen1 capacity, a Power BI admin must enable paginated reports in the Premium capacities section of the Power BI admin portal. Comparison between Power BI Reports and Paginated Report Paginated reports doesn’t possess built-in data models like Power BI reports. A paginated report contains various built-in chart options including table, matrix, gauge, maps, sparklines, and different other chart types The supremacy of paginated reports is their ability to print all the data in a table, no matter how extended it takes. Also, printing is easier in a paginated report for a table, for instance, when a user prints or exports to PDF, the paginated report can contain as many pages necessary to print every row in the table. The Use of Report Builder Paginated reports possess its own design tool; this design tool is known as the Power BI Report Builder. This tool has same functionality as the tools used for creating paginated reports for Power BI Report Server or SQL Server Reporting Services(SSRS).With the use of this report builder we can preview the report before publishing to the Microsoft Power BI service. Features of Power BI Report Builder Data Modification: A user can group, filter, and sort data for paginated reports when using the report builder. A user can as well add formulas to their reports. Report Modification: Power BI Report Builder allows users to update and customize the reports created with the SQL Server Data Tools (SSDT) Report Designer. Multiple Layouts: A user can as well create Paginated Reports for matrix reports, column-based data for summarized data, chart reports for graph-based data e.t.c. Reporting from Multiple Sources: It also allows users to create reports that draws relational and multidimensional data from various sources like SQL Server and Analysis Services, Power BI datasets, etc. Data Sources With a single paginated report we can have as many different data sources and this makes it differ from Power BI Reports Data Model. We can also create embedded data sources and datasets inside the paginated report. These are some of the data sources we can connect to: Power BI datasets Oracle Teradata Azure SQL Database and Azure Synapse Analytics (via Basic and OAuth) Azure Analysis Services (via SSO) SQL Server via a gateway We can also export paginated reports in any of this various formats such as; Excel / CSV, PDF, Word, HTML and MHTML, XML. Today’s article introduced you to the basic things you need to know to start using paginated reports. We hope you will begin to explore all its features. Comment down below your observations or questions while using paginated reports in Power BI. Thanks for reading.

Scroll to Top