How To Combine Text And CSV Files With Python

Python Logo

Following on from my How To Combine Text And CSV Files With Python article I decided to create a script to combine CSV or Text files into one file. This isn’t something I need to do on a regular basis but when I do automating it saves a lot of time.

The script is fairly basic. It opens each file within a specified directory, reads the content and appends it to a specified output file.

Continue reading

How To Split Text And CSV Files With Python

Python Logo

Despite the march of technology plain text files are still a popular way to transfer and process data. Comma Separated Value (CSV) files are still used to import and export data between spreadsheets, databases, online tools and other data repositories.

The script shown below will take a text file and split it into a number of smaller files based on a specified line count. This works for normal text as well as CSV files. I use it to split large data sets into smaller batches for import into database systems.

Continue reading

How To Split A List Into Chunks In Python

Python Logo

Lists are a useful concept in Python and in many ways are similar to arrays in other programming languages. Python provides a number of list methods which allow you to manipulate the elements within and these are documented in the official data structures documentation.

One thing I needed to do recently was to take a list of data and split it into a number of smaller chunks. I wanted to specify the number of the chunks and the elements of the list to be split appropriately,

In the example below the function “get_chunks” is defined. It accepts two arguments. The first is a list of data and the second is the number of chunks you require.

Continue reading

How To Generate Random Numbers In Python

Python Logo

Python includes a standard library to generate random numbers. This library is called “random”. In order to start generating random numbers in Python you must import the random library at the start of your script.

When you import the library the generator is seeded using the current system time so you may see different results every time you run your script.

Continue reading

How To Resize Images Using Python

Python Logo

One task I use Python to perform on my desktop is image manipulation. Although there are plenty of good image processor utilities (i.e. IrfanView and XnView) sometimes it is simply quicker to create a Python script to complete a particular task for you.

By importing the Image library and making use of the resize function images can be resized to a specified size. It supports all common image formats such as PNG, GIF, JPG and BMP.

Continue reading

How To Send Text Emails In Python

Python Logo

Sending email in Python is easy if you have access to an SMTP server. These are often provided as part of a web hosting account or by email providers. The details are those that would be required to send email using Outlook, Thunderbird or other third-party mail client.

The required SMTP details are:

  • SMTP server domain
  • Username
  • Password

Continue reading