Tuples in Python

In this article, we will see what are Tuple and how we use them in Python programming.

Tuples:

 

Tuples are one of the built-in types of sequences out of six and just like lists. There is another article published on how to use Lists in Python. We want to store the values for the rest of our program then we can use Tuples.  Now for an example, if we want to store the name of some category of any Test plan when comes to compliance like “Audit, SOX, Risk Management or ORM” for continuing some program.

First, we need to declare a tuple and give it a name, the initial values will be used within parentheses “()”. All the values will be separated by a comma………continue reading from my original article that was published on Experts-Exchange.com and provides a basic explanation on how to use Tuples in Python programming language.

 

Thank you for reading my blog, the reference is taken from one of my article that was published by Experts-Exchange.

Please feel free to leave me some feedback or to suggest any future topics.

Looking forward to hearing from you – Swadhin Ray (Sloba) -( LinkedIn ) ( Twitter )

Advertisements

Logical Operators in Python

In this article, we will see what is the logical operator and how we use them in Python programming.

Logical Operators:

There are basically three types of logical operators as below:

  • And
  • Or
  • Not

There above operators semantics or meaning is the same as their meaning in English.  To demonstrate we can take one simple example like a number can be greater then 2 and less then 5, so if we consider this example then we know that there could be only two possible numbers comes in between i.e. 3 and 4. So now let us run this example and verify the results by passing different numbers……….continue reading from my original article that was published on Experts-Exchange.com and provides a basic explanation on how to use Logical Operators in Python programming language.

 

Thank you for reading my blog, the reference is taken from one of my article that was published by Experts-Exchange.

Please feel free to leave me some feedback or to suggest any future topics.

Looking forward to hearing from you – Swadhin Ray (Sloba) -( LinkedIn ) ( Twitter )

Boolean in Python

In this article, we will see how to use Boolean in Python where it can be used to take any decisions if it is true or false, yes or no,  1 or 0.

What is Boolean?

It is just an integer that can have 1, 0,-1 or floats like 0.32, 3.32 etc. The output of the decisions can be true or false.

In many cases during writing code or implementing any comparison to consider any decision we usually use boolean in any programming language.  ………continue reading from my original article that was published on Experts-Exchange.com and provides a basic explanation on how to use Boolean in Python programming language.

 

Thank you for reading my blog, the reference is taken from one of my article that was published by Experts-Exchange.

Please feel free to leave me some feedback or to suggest any future topics.

Looking forward to hear from you – Swadhin Ray (Sloba) -( LinkedIn ) ( Twitter )

Install JDK9 on Linux Mint

In this article we will see how we can install JDK9 i.e. Oracle Java 9 on Linux mint version.

Execute the below commands in sequence on Linux Mint machine:

sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java9-installer

While executing the above you will see the get the aggrement options where we need to click ok and proceed. After the above steps setup Java environment using PPA repository

Exeucte the below command to install the package :

sudo apt-get install oracle-java9-set-default

Now let us check our Java version by exeucting the below commands:

oracle@dev-VirtualBox ~ $ java -version
java version "9.0.4"
Java(TM) SE Runtime Environment (build 9.0.4+11)
Java HotSpot(TM) 64-Bit Server VM (build 9.0.4+11, mixed mode)
oracle@dev-VirtualBox ~ $ 

Thank you for reading my blog , please feel free to leave me some feedback or to suggest any future topics.

Looking forward to hear from you – Swadhin Ray (Sloba) -( LinkedIn ) ( Twitter )

 

0% [Connecting to archive.ubuntu.com ] Error

I encountered this error while using “apt-get” command on  my Linux mint server and it got stuck for 12-3 mins.

Here is the details on my error that I faced :


dev@dev-VirtualBox /etc $ sudo apt-get install alien
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
autoconf automake autopoint autotools-dev debhelper debugedit dh-autoreconf
dh-strip-nondeterminism libfile-stripnondeterminism-perl librpm3
librpmbuild3 librpmio3 librpmsign3 libtool po-debconf rpm rpm-common
rpm2cpio

.........

......

.........

0% [Connecting to archive.ubuntu.com (2001:67c:1560:8001::14)] [Connecting to

The issue is on the IPv6 taking more time to execute. Now let us check if my IPv6 is enabled or disabled. Based on the output we will see if we need to disable.


dev@dev-VirtualBox /etc $ cat /proc/sys/net/ipv6/conf/all/disable_ipv6
0
dev@dev-VirtualBox /etc $

If we get the output “0” then my IPv6 is enabled and if we get “1” then it is disabled.  So in my case I have to disable it. Let us see how we can disable it . And try to rerun my command where I got stuck.

To disable please execute the below commands and after that reboot the machine:


dev@dev-VirtualBox /etc $ echo "#disable ipv6" | sudo tee -a /etc/sysctl.conf
#disable ipv6
dev@dev-VirtualBox /etc $ echo "net.ipv6.conf.all.disable_ipv6 = 1" | sudo tee -a /etc/sysctl.conf
net.ipv6.conf.all.disable_ipv6 = 1
dev@dev-VirtualBox /etc $ echo "net.ipv6.conf.default.disable_ipv6 = 1" | sudo tee -a /etc/sysctl.conf
net.ipv6.conf.default.disable_ipv6 = 1
dev@dev-VirtualBox /etc $ echo "net.ipv6.conf.lo.disable_ipv6 = 1" | sudo tee -a /etc/sysctl.conf
net.ipv6.conf.lo.disable_ipv6 = 1
dev@dev-VirtualBox /etc $

I have rebooted my system now let us check what is the output I am getting :


oracle@dev-VirtualBox ~ $ cat /proc/sys/net/ipv6/conf/all/disable_ipv6
1
oracle@dev-VirtualBox ~ $

Now we can see that my IPv6 is disabled , so let us try to execute my initial “apt-get” command


dev@dev-VirtualBox ~ $ sudo apt-get update
Hit:1 http://archive.canonical.com/ubuntu xenial InRelease
Ign:2 http://packages.linuxmint.com sylvia InRelease
Hit:3 http://packages.linuxmint.com sylvia Release
Hit:4 http://archive.ubuntu.com/ubuntu xenial InRelea

...

...

..

Get:22 http://archive.ubuntu.com/ubuntu xenial-updates/universe Translation-en [233 kB]
Get:23 http://archive.ubuntu.com/ubuntu xenial-updates/multiverse amd64 Packages [16.2 kB]
Get:24 http://archive.ubuntu.com/ubuntu xenial-updates/multiverse i386 Packages [15.3 kB]
Fetched 4,825 kB in 1s (2,621 kB/s)
Reading package lists... Done
dev@dev-VirtualBox ~ $

So we can see that we are now able to install the packages without any issues.

 

Thank you for reading my blog , please feel free to leave me some feedback or to suggest any future topics.

Looking forward to hear from you – Swadhin Ray (Sloba) -( LinkedIn ) ( Twitter )

 

 

Append row into a CSV file on SFTP

In this post I am going to show how we can append a row into CSV file using simple bash command.

To demonstrate this I am using Linux virtual machine, the OS is on Mint 18.3 64bit.  First me create a sample file on my Linux Machine as like below.

dev@dev-VirtualBox ~/Desktop/CSV $ pwd
/home/dev/Desktop/CSV
dev@dev-VirtualBox ~/Desktop/CSV $ ls
dev@dev-VirtualBox ~/Desktop/CSV $ cat <<EOF >sample.csv
> COL1,COL2,COL3
> 1,FIRST_LINE,18-JAN-2018
> EOF
dev@dev-VirtualBox ~/Desktop/CSV $ ls
sample.csv
dev@dev-VirtualBox ~/Desktop/CSV $

Once the file is created it will look something like below:

image

Now let us think that this file is on a SFTP server where we need to connect and append another line without renaming or changing the file format.

Login to the server


login as: dev
dev@10.0.0.203's password:
Welcome to Linux Mint 18.3 Sylvia (GNU/Linux 4.10.0-38-generic x86_64)

* Documentation:  <a href="https://www.linuxmint.com/">https://www.linuxmint.com</a>
Last login: Tue Jan 23 09:55:52 2018 from 10.0.0.203
dev@dev-VirtualBox ~ $ ls
Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos
dev@dev-VirtualBox ~ $ cd Desktop/
dev@dev-VirtualBox ~/Desktop $ ls
CSV
dev@dev-VirtualBox ~/Desktop $ cd CSV/
dev@dev-VirtualBox ~/Desktop/CSV $ ls
sample.csv

So now we can login to the server.

Method 1: Now let us execute the below line to insert or append a new row to the existing CSV file i.e. sample.csv:


dev@dev-VirtualBox ~/Desktop/CSV $ sed -i.bak 1i"2,Second_Line,18-JAN-2018" sample.csv
dev@dev-VirtualBox ~/Desktop/CSV $

Once the above line is executed we will see the row is inserted but not at the end but at the beginning.

image

Now how we can append the line at the end .


dev@dev-VirtualBox ~/Desktop/CSV $ pwd
/home/dev/Desktop/CSV
dev@dev-VirtualBox ~/Desktop/CSV $ ls
sample.csv
dev@dev-VirtualBox ~/Desktop/CSV $ cat sample.csv
COL1,COL2,COL3
1,FIRST_LINE,18-JAN-2018
dev@dev-VirtualBox ~/Desktop/CSV $ sed -i -e "\$a2,Second_Line,18-JAN-2018" sample.csv
dev@dev-VirtualBox ~/Desktop/CSV $ cat sample.csv
COL1,COL2,COL3
1,FIRST_LINE,18-JAN-2018
2,Second_Line,18-JAN-2018
dev@dev-VirtualBox ~/Desktop/CSV $

 

The line used to insert the row/ append the row at the end is :

sed -i -e "\$a2,Second_Line,18-JAN-2018" sample.csv

Method 2: Now let us check how we can use echo to insert the line at the end.


dev@dev-VirtualBox ~/Desktop/CSV $ echo "3,Third_Line,18-JAN-2018" >> sample.csvdev@dev-VirtualBox ~/Desktop/CSV $ cat sample.csv
COL1,COL2,COL3
1,FIRST_LINE,18-JAN-2018
2,Second_Line,18-JAN-2018
3,Third_Line,18-JAN-2018
dev@dev-VirtualBox ~/Desktop/CSV $

Method 3: Append using a simple python programming


dev@dev-VirtualBox ~/Desktop/CSV $ python
Python 2.7.12 (default, Nov 19 2016, 06:48:10)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> with open('sample.csv', 'a') as file:
...     file.write('4,Fourth_Line,18-JAN-2018')
... ;
   File "<stdin>", line 3
     ;
     ^
SyntaxError: invalid syntax
>>> with open('sample.csv', 'a') as file:
...     file.write('4,Fourth_Line,18-JAN-2018\n');
...
>>> exit()

Finally, the file will look like as below:


dev@dev-VirtualBox ~/Desktop/CSV $ cat sample.csv
COL1,COL2,COL3
1,FIRST_LINE,18-JAN-2018
2,Second_Line,18-JAN-2018
3,Third_Line,18-JAN-2018
4,Fourth_Line,18-JAN-2018

dev@dev-VirtualBox ~/Desktop/CSV $



 

image

 

To automate the process you can create a script and try to pass the row as a input to the file while calling the script.

 

Thank you for reading my blog , please feel free to leave me some feedback or to suggest any future topics.

Looking forward to hear from you – Swadhin Ray (Sloba) -( LinkedIn ) ( Twitter )

 

Reorder the columns in CSV file using Python

Many time we work on files to load or process the flat files. In this post, I am showing if we receive the file but the columns are not in order then how to rearrange them in the right format. There are multiple ways to handle this but here I am using a simple python code which will reorder my columns.

Say for example my right order of my file should be as below:

 empno, ename, sal 

But say I receive a file with

 sal, ename, empno 

image

Now here is the simple python script which will change the file into right order :

import csv

with open('file2.csv', 'r') as infile, open('file2_modified.csv', 'a') as outfile:
fieldnames = ['empno','ename','sal']
writer = csv.DictWriter(outfile, fieldnames=fieldnames)
writer.writeheader()
for row in csv.DictReader(infile):
writer.writerow(row)

Save the above code as “target_file_format.py” or anyname you want.

Execute the python script , and check the result.

image

You can modify the code in various ways as per your need.

Thank you for reading my blog , please feel free to leave me some feedback or to suggest any future topics.

Looking forward to hear from you – Swadhin Ray (Sloba) -( LinkedIn ) ( Twitter )