Author Archives: admin

Split on Whitespace in Python

Whitespace is a character or set of characters that represents vertical or horizontal space.

The split function takes a single optional argument. If you use this function without a parameter, it separates words by single or series of whitespace characters, as long as there is no other character between them.

Continue reading

Convert Datetime to Timestamp in Python

To convert datetime to timestamp, first, you have to import the datetime module, assign datetime to a variable and use the timestamp function to convert it into a timestamp.

from datetime import datetime

my_datetime = datetime(2021, 6, 14, 12, 33, 41)
timestamp = my_datetime.timestamp()

print("timestamp:", timestamp)
print("datetime:", my_datetime)

If you run the code, both, the timestamp and datetime are printed.

timestamp: 1623666821.0
datetime: 2021-06-14 12:33:41

Convert timestamp to datetime

To convert a timestamp to a datetime, you have to assign the timestamp to a variable. Next, the variable has to be converted to the datetime object.

from datetime import datetime

timestamp = 1435634284
my_datetime = datetime.fromtimestamp(timestamp)

print("datetime:", my_datetime)
print("timestamp:", timestamp)
print("type(my_datetime): ", type(my_datetime))

If you run the code, you’ll see that the supposedly meaningless string of characters returned date and time.

The last line prints the type of the variable.

datetime: 2015-06-30 05:18:04
timestamp: 1435634284

Convert the current timestamp

In the previous example, we used a ti+mestamp value that returned a date somewhere in 2015. If you want to get the current timestamp, you can get the current datetime and convert it to the timestamp, using the time() function.

from datetime import datetime
from time import time

timestamp = time()
my_datetime = datetime.fromtimestamp(timestamp)

print("timestamp:", timestamp)
print("datetime:", my_datetime)
print("type(my_datetime): ", type(my_datetime))

This code returns the current timestamp represented by the float value.

timestamp: 1623686864.2045426
datetime: 2021-06-14 18:07:44.204543

Create CSV File in PowerShell

To create a CSV file in PowerShell, you can use the following set of commands:

New-Item file.csv
Set-Content file.csv 'First Name,Last Name,Age'

This is how it looks like inside PowerShell.

The first line creates a file and the second one adds three values: First Name, Last Name, Age.

If you open a file in a text editor, the three values are going to be separated by a delimiter. In our case, it’s a comma.

Since the CSV file is just a text file separated by a delimiter (comma in our example), you can easily open this file in a text editor.

It’s the exact text we typed in the terminal.

If you try to add another text with the Set-Content cmdlet, the value that you added earlier will be overwritten at the first line.

Instead, we are going to use a different cmdlet, called Add-Content.

Add-Content file.csv 'John,Doe,54'
Add-Content file.csv 'Emilia,Plater,25'

Two new rows are added. Each row is added after the previous one. Therefore the rows are inserted into rows 2 and 3.

You can open this file in Excel, and you are going to get each value separated by a comma inside a different cell unless you changed the default separator.

Create the file using the foreach loop

Instead of inserting one line per one command, you can use the foreach loop. First, you have to use Set-Content to create a file and add a header, then it adds values from the list using the foreach loop.

Set-Content file.csv 'First Name,Last Name,Age'
$people = @(
$people | foreach { Add-Content file.csv -Value $_ }

The result of this example is the same as before.

Checking the CSV file

We created a CSV file. Now, we can check its structure with the Import-CSV cmdlet.

Import-Csv file.csv

The contents of the file are printed on the terminal, so we can be sure that the structure is correct.