Posted on Leave a comment

ModuleNotFoundError: No Module Named ‘ffmpeg’ (Fixed)

5/5 – (1 vote)

Quick Fix: Python raises the ModuleNotFoundError: No module named 'ffmpeg' when it cannot find the library ffmpeg. The most frequent source of this error is that you haven’t installed ffmpeg explicitly with pip install ffmpeg-python or even pip3 install ffmpeg-python for Python 3. Alternatively, you may have different Python versions on your computer, and ffmpeg is not installed for the particular version you’re using.

Problem Formulation

You’ve just learned about the awesome capabilities of the ffmpeg library and you want to try it out, so you start your code with the following statement:

import ffmpeg
stream = ffmpeg.input('input.mp4')
stream = ffmpeg.hflip(stream)
stream = ffmpeg.output(stream, 'output.mp4')
ffmpeg.run(stream)

The first line is supposed to import the ffmpeg library into your (virtual) environment. However, it only throws the following ImportError: No module named ffmpeg:

>>> import ffmpeg
Traceback (most recent call last): File "<pyshell#6>", line 1, in <module> import ffmpeg
ModuleNotFoundError: No module named 'ffmpeg'

Solution Idea 1: Install Library ffmpeg

The most likely reason is that Python doesn’t provide ffmpeg in its standard library. You need to install it first!

Before being able to import the ffmpeg module, you need to install it using Python’s package manager pip. Make sure pip is installed on your machine.

To fix this error, you can run the following command in your Windows shell:

$ pip install ffmpeg-python

This simple command installs ffmpeg in your virtual environment on Windows, Linux, and MacOS. It assumes that your pip version is updated. If it isn’t, use the following two commands in your terminal, command line, or shell (there’s no harm in doing it anyways):

$ python -m pip install – upgrade pip
$ pip install ffmpeg-python

💡 Note: Don’t copy and paste the $ symbol. This is just to illustrate that you run it in your shell/terminal/command line.

Solution Idea 2: Fix the Path

The error might persist even after you have installed the ffmpeg library. This likely happens because pip is installed but doesn’t reside in the path you can use. Although pip may be installed on your system the script is unable to locate it. Therefore, it is unable to install the library using pip in the correct path.

To fix the problem with the path in Windows follow the steps given next.

Step 1: Open the folder where you installed Python by opening the command prompt and typing where python

Step 2: Once you have opened the Python folder, browse and open the Scripts folder and copy its location. Also verify that the folder contains the pip file.

Step 3: Now open the Scripts directory in the command prompt using the cd command and the location that you copied previously.

Step 4: Now install the library using pip install ffmpeg-python command. Here’s an analogous example:

After having followed the above steps, execute our script once again. And you should get the desired output.

Other Solution Ideas

  • The ModuleNotFoundError may appear due to relative imports. You can learn everything about relative imports and how to create your own module in this article.
  • You may have mixed up Python and pip versions on your machine. In this case, to install ffmpeg for Python 3, you may want to try python3 -m pip install ffmpeg-python or even pip3 install ffmpeg-python instead of pip install ffmpeg-python
  • If you face this issue server-side, you may want to try the command pip install – user ffmpeg-python
  • If you’re using Ubuntu, you may want to try this command: sudo apt install ffmpeg-python
  • You can also check out this article to learn more about possible problems that may lead to an error when importing a library.

Understanding the “import” Statement

import ffmpeg

In Python, the import statement serves two main purposes:

  • Search the module by its name, load it, and initialize it.
  • Define a name in the local namespace within the scope of the import statement. This local name is then used to reference the accessed module throughout the code.

What’s the Difference Between ImportError and ModuleNotFoundError?

What’s the difference between ImportError and ModuleNotFoundError?

Python defines an error hierarchy, so some error classes inherit from other error classes. In our case, the ModuleNotFoundError is a subclass of the ImportError class.

You can see this in this screenshot from the docs:

You can also check this relationship using the issubclass() built-in function:

>>> issubclass(ModuleNotFoundError, ImportError)
True

Specifically, Python raises the ModuleNotFoundError if the module (e.g., ffmpeg) cannot be found. If it can be found, there may be a problem loading the module or some specific files within the module. In those cases, Python would raise an ImportError.

If an import statement cannot import a module, it raises an ImportError. This may occur because of a faulty installation or an invalid path. In Python 3.6 or newer, this will usually raise a ModuleNotFoundError.

Related Videos

The following video shows you how to resolve the ImportError:

YouTube Video

The following video shows you how to import a function from another folder—doing it the wrong way often results in the ModuleNotFoundError:

YouTube Video

How to Fix “ModuleNotFoundError: No module named ‘ffmpeg’” in PyCharm

If you create a new Python project in PyCharm and try to import the ffmpeg library, it’ll raise the following error message:

Traceback (most recent call last): File "C:/Users/.../main.py", line 1, in <module> import ffmpeg
ModuleNotFoundError: No module named 'ffmpeg' Process finished with exit code 1

The reason is that each PyCharm project, per default, creates a virtual environment in which you can install custom Python modules. But the virtual environment is initially empty—even if you’ve already installed ffmpeg on your computer!

Here’s a screenshot exemplifying this for the pandas library. It’ll look similar for ffmpeg-python.

The fix is simple: Use the PyCharm installation tooltips to install Pandas in your virtual environment—two clicks and you’re good to go!

First, right-click on the pandas text in your editor:

Second, click “Show Context Actions” in your context menu. In the new menu that arises, click “Install Pandas” and wait for PyCharm to finish the installation.

The code will run after your installation completes successfully.

As an alternative, you can also open the Terminal tool at the bottom and type:

$ pip install ffmpeg-python

If this doesn’t work, you may want to set the Python interpreter to another version using the following tutorial: https://www.jetbrains.com/help/pycharm/2016.1/configuring-python-interpreter-for-a-project.html

You can also manually install a new library such as ffmpeg in PyCharm using the following procedure:

  • Open File > Settings > Project from the PyCharm menu.
  • Select your current project.
  • Click the Python Interpreter tab within your project tab.
  • Click the small + symbol to add a new library to the project.
  • Now type in the library to be installed, in your example Pandas, and click Install Package.
  • Wait for the installation to terminate and close all popup windows.

Here’s an analogous example:

Here’s a full guide on how to install a library on PyCharm.

Posted on Leave a comment

Python decode()

5/5 – (1 vote)

This tutorial explains the Python decode() method with arguments and examples. Before we dive into the Python decode() method, let’s first build some background knowledge about encoding and decoding so you can better understand its purpose. 👇

Encoding and Decoding – What Does It Mean?

Programs must handle various characters in several languages. Application developers often internationalize programs to display messages and error outputs in various languages, be it English, Russian, Japanese, French, or Hebrew. 

Python’s string type uses the Unicode Standard to represent characters, which lets Python programs work with all possible characters.

Unicode aims to list every character used by human languages and gives each character its unique code. The Unicode Consortium specifications regularly update its specifications for new languages and symbols.

A character is the smallest component of the text. For example, ’a, ‘B’, ‘c’, ‘È’ and ‘Í’ are different characters. Characters vary depending on language or context. For example, the character for “Roman Numeral One” is ‘Ⅰ’, separate from the uppercase letter ‘I’. Though they look the same, these are two different characters that have different meanings.

The Unicode standard describes how code points represent characters. A code point value is an integer from 0 to 0x10FFFF. [1]

What are Encodings?

A sequence of code points forms a Unicode String represented in memory as a set of code units. These code units are mapped to 8-bit bytes. Character Encoding is the set of rules to translate a Unicode string to a byte sequence.

UTF-8 is the most commonly used encoding, and Python defaults to it. UTF stands for “Unicode Transformation Format”, and the ‘8’ refers to 8-bit values used in the encoding. [2]

Python decode()

Encoders and decoders convert text between different representations, and specifically, the Python bytes decode() function converts bytes to string objects.

The decode() method converts/decodes from one encoding scheme for the argument string to the desired encoding scheme. It is the opposite of the Python encode() method.

decode() accepts the encoding of the encoded string, decodes it, and returns the original string.

Here’s the syntax of the method:

decode(encoding, error)
str.decode([encoding[, errors]]) # Example:
str.decode(encoding='UTF-8',errors='strict'

The decode() arguments:

Argument Description
encoding (optional) Specifies the encoding to decode. Standard Encodings has a list of all encodings.
errors (optional) Decides how to handle the errors:

'strict' [default], meaning encoding errors raise a UnicodeError. 

Other possible values are:

'ignore' – Ignore the character and continue with the next

'replace' – Replace with a suitable replacement character

'xmlcharrefreplace' – Inserts an XML character reference 

'backslashreplace' – Inserts a backslash escape sequence (\uNNNN) instead of un-encodable Unicode characters

'namereplace' – Inserts a \N{...} escape sequence and any other name registered via codecs.register_error()

Example 1

text = "Python Decode converts text string from one encoding scheme to the desired one."
encoded_text = text.encode('ubtf8', 'strict')
print("Encoded String: ", encoded_text)
print("Decoded String: ", encoded_text.decode('utf8', 'strict'))
  • Encoded Stringb'Python Decode converts text from one encoding scheme to desired encoding scheme.'
  • Decoded StringPython Decode converts text from one encoding scheme to desired encoding scheme.

Example 2

>>> b'\x81abc'.decode("utf-8", "strict")
Traceback (most recent call last): File "<pyshell#55>", line 1, in <module> b'\x81abc'.decode("utf-8", "strict")
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x81 in position 0: invalid start byte
>>> b'\x80abc'.decode("utf-8", "backslashreplace") '\\x80abc'
>>> b'\x80abc'.decode("utf-8", "ignore") 'abc'

References

Posted on Leave a comment

Solidity Ether Units, Time Units, and Global Variables

5/5 – (1 vote)
YouTube Video

With this article, we’re opening a new area of our study, that of units and globally available variables in Solidity.

To begin with, we’ll learn about ether units and time units. After that, we’ll start with a block of subsections on special variables and functions, stretching through this and the next two articles.

It’s part of our long-standing tradition to make this (and other) articles a faithful companion, or a supplement to the official Solidity documentation.

Ether Units

When mentioning Ether units of currency, we can express them with a literal number and a suffix of wei, gwei or ether.

These suffixes specify a sub-denomination of Ether. We assume amounts written without a suffix as Wei.

The purpose and effect of using sub-denomination is a multiplication of the denomination by a power (exponent) of ten.

assert(1 wei == 1);
assert(1 gwei == 1e9);
assert(1 ether == 1e18);

💡 Note: There can be found denominations, such as finney and szabo, but they were deprecated in Solidity v0.7.0.

Time Units

Solidity has a nice and natural way of expressing time units with suffixes, such as seconds, minutes, hours, days, and weeks.

Seconds are the base unit, and units are considered to correspond to:

  • 1 == 1 seconds
  • 1 minutes == 60 seconds
  • 1 hours == 60 minutes
  • 1 days == 24 hours
  • 1 weeks == 7 days

When working with calendar calculations using these units, we should take extra care, because only some years have 365 days, and because of leap seconds, not even every day has 24 hours.

Since leap seconds are unpredictable, an exact calendar always has to be updated by an external source (oracle), which motivated Solidity authors to remove the suffix years in Solidity v0.5.0.

We should remember that these suffixes cannot be applied to variables, meaning if we want to interpret a function parameter expressed in days, we can easily do so like in the example below:

function f(uint start, uint daysAfter) public { if (block.timestamp >= start + daysAfter * 1 days) { // ... }
}

Special Variables and Functions

The global namespace contains special variables and functions primarily used to provide us with information about the blockchain. They are also available utility functions for general use.

Block and Transaction Properties

The following is a list of block and transaction properties, as shown  in the official Solidity documentation. Parentheses next to each block/transaction property define the member type:

  • blockhash(uint blockNumber) returns (bytes32): hash of the given block when blocknumber is one of the 256 most recent blocks; otherwise returns zero
  • block.basefee (uint): current block’s base fee (as defined in Ethereum Improvement Proposals EIP-3198 and EIP-1559)
  • block.chainid (uint): current chain id
  • block.coinbase (address payable): current block miner’s address
  • block.difficulty (uint): current block difficulty
  • block.gaslimit (uint): current block gas limit
  • block.number (uint): current block number
  • block.timestamp (uint): current block timestamp as seconds since Unix epoch
  • gasleft() returns (uint256): remaining gas
  • msg.data (bytes calldata): complete calldata
  • msg.sender (address): the sender of the message (current call)
  • msg.sig (bytes4): first four bytes of the calldata (i.e. function identifier)
  • msg.value (uint): number of Wei sent with the message
  • tx.gasprice (uint): the gas price of the transaction
  • tx.origin (address): the sender of the transaction (full call chain)

Note: We should expect the values of all members of msg, including msg.sender and msg.value will change with every external function call. This expectation also applies to library functions.

💡 Info: Off-chain computation is simply a computation that takes place outside a blockchain. Oracle networks can provide a trust-minimized form of off-chain computation to extend the capabilities of blockchains – this is known as oracle computation. (Chainlink)

Contracts can be evaluated both off-chain and on-chain, i.e. in the context of a transaction included in a block.

If a contract is evaluated off-chain, we should assume that block.* and tx.* members don’t refer to a specific block or transaction. Instead, the values we’d find in these members are provided by the EVM implementation executing the contract, and therefore, they can be completely arbitrary.

🗒 Note: It is suggested to avoid block.timestamp or blockhash member values as sources of randomness. The reason for this suggestion lies in the fact that, in some instances, miners can manipulate both the timestamp and the block hash. The timestamp of the current block must be strictly larger than the timestamp of the last block, and the only thing we can know for sure is that it will be between the timestamps of two neighboring blocks in the canonical chain; how far it will be from any particular block timestamp is not known.

💡 Info: “The word canonical is used to indicate a particular choice from a number of possible conventions. This convention allows a mathematical object or class of objects to be uniquely identified or standardized.” (Wolfram.com)

Only the recent 256 blocks’ hashes are available, due to scalability reasons. A hash of any older block will be zero.

In Solidity versions prior to 0.5, the current blockhash(...) function was previously known and available as block.blockhash(...); the current gasleft(...) function was previously known and available as msg.gas(...).

In Solidity v0.7.0 the now alias (for block.timestamp) was removed.

ABI Encoding and Decoding Functions

The following list contains ABI-appropriate functions for low-level interactions with EVM, as laid out in the official Solidity documentation:

  • abi.decode(bytes memory encodedData, (...)) returns (...): ABI-decodes the given data, while the types are given in parentheses as second argument. Example: (uint a, uint[2] memory b, bytes memory c) = abi.decode(data, (uint, uint[2], bytes))
  • abi.encode(...) returns (bytes memory): ABI-encodes the given arguments
  • abi.encodePacked(...) returns (bytes memory): Performs packed encoding of the given arguments. Note that packed encoding can be ambiguous!
  • abi.encodeWithSelector(bytes4 selector, ...) returns (bytes memory): ABI-encodes the given arguments starting from the second and prepends the given four-byte selector
  • abi.encodeWithSignature(string memory signature, ...) returns (bytes memory): Equivalent to abi.encodeWithSelector(bytes4(keccak256(bytes(signature))), ...)
  • abi.encodeCall(function functionPointer, (...)) returns (bytes memory): ABI-encodes a call to functionPointer with the arguments found in the tuple. Performs a full type-check, ensuring the types match the function signature. Equivalent to abi.encodeWithSelector(functionPointer.selector, (...))

🗒 Note: “These encoding functions can be used to craft data for external function calls without actually calling an external function. Furthermore, keccak256(abi.encodePacked(a, b)) is a way to compute the hash of structured data (although be aware that it is possible to craft a “hash collision” using different function parameter types).” (docs)

Conclusion

In this article, we learned about ether and time units, followed by special variables and functions.

First, we introduced ether units and discussed the use of the main unit and its sub-denominations.

Second, we introduced time units and discussed the possibilities of expressing time in different time units.

Third, we took a closer look at the block and transaction properties, but also listed many of them with descriptions and notes on specific behaviors for a more thorough understanding.

Fourth, we also touched on the topic of ABI encoding and decoding functions, described them, and gave a usage hint in a form of a note.

What’s Next?

This tutorial is part of our extended Solidity documentation with videos and more accessible examples and explanations. You can navigate the series here (all links open in a new tab):

Posted on Leave a comment

Getting To Know Your Basic UCTRONICS Raspberry Pi Pico Kit

5/5 – (1 vote)

Now that we’ve covered Thonny and MicroPython, it’s time to get to know the components in your basic Raspberry Pi Pico kit.

🤖 Note: There is more than one kit out there, but it is this author’s opinion that the best value for beginners is the one from UCTRONICS. Most of them have similar components, but this one just happens to be my go-to and is the subject of this installment.

*If you have not yet read our previous installments on learning MicroPython for the Raspberry Pi Pico or Thonny, be sure you review those before moving beyond this short tutorial.

Alright, let’s jump in! Here’s what the kit looks like:

First things first, though. We’re going to start with the Pico itself. The main thing you’ll need to know about it (at least for the purposes of doing experiments) is its pins.

Your Pico and Its Pins

Your Pico talks to hardware through a series of pins along both its edges. Most of these pins work as a general-purpose input/output (GPIO) pin, meaning they can be programmed to act as either an input or an output and have no fixed purpose of their own.

  • Some pins have extra features and alternative modes for communicating with more complicated hardware;
  • Others have a fixed purpose, providing connections for things like power.

Raspberry Pi Pico’s 40 pins are labeled on the underside of the board, with three also labeled with their numbers on the top of the board: Pin 1, Pin 2, and Pin 39.

These top labels help you remember how the numbering works: Pin 1 is at the top left as you look at the board from above with the micro USB port to the upper side, Pin 20 is the bottom left, Pin 21 the bottom right, and Pin 39 one below the top right with the unlabeled Pin 40 above it.

While you will generally use actual pin numbers when writing your code, we may reference some of them by their functions, as well.

As you can see, there are several different kinds of pins with different functions. Here’s a quick reference guide to help you remember:

You’ll learn more about these functions in later tutorials as we get into projects. For now, all you need to focus on is the basics. This is not meant to be an exhaustive electronics course. 🙂

Other Electronic Components

Obviously, your Pico isn’t the only thing you’re going to need if you’re going to be conducting any experiments.

The other things are the components that your Pico’s pins will be controlling. There are tons of components out there, but the cool thing about this kit is that these are useful regardless of how complicated your projects ultimately get – and if you have fun with these, I guarantee you’ll start coming up with your own.

Breadboard

Anyway, let’s start with the second most important component (well, besides the micro USB cord that connects the Pico to your computer) – your breadboard

This little beauty makes life a WHOLE lot easier. This unit eliminates the need for a circuit board and soldering. Instead, you can just shove – ok, maybe not shove, but insert – wires and pins into the right spots and remove them as needed to do new projects. Hard to beat that!

Jumper Wires

Next are your jumper wires, which connect components to circuits on your breadboard and take the place of those little “wire trails” you see on circuit boards.

That way, they aren’t permanent and can be moved as you see fit. They can also be used to lengthen wires or pins as needed.

To accomplish this, they come in 3 types – male-to-male (M2M), male-to-female (M2F), and of course F2F (can you guess?).

Momentary Switch

Next up, we have a push-button switch, or momentary switch. This doodad is exactly what it looks like – a button.

However, in this case, it’s not the same as a latching switch, which stays depressed once you click it. This one only has to be held down to make it keep working.

Now this one’s going to shock you. It’s called… a light-emitting diode, or LED! I know, I know, you’ve never heard of it, right? 😂

I don’t think I have to say much of anything about these, but I will make two points.

  • First, not all LEDs are going to work with your Pico. 5V and 12V aren’t good, so if you decide to buy more, make sure you keep that in mind.
  • Second, the two legs are different lengths. The long one is positive, and the short is negative.

When we’re running a current through stuff, we can sometimes run the risk of blowing them out, especially the LEDs. So how do we prevent that from happening?

Resistor

With resistors, of course! Now, there are a lot of resistors out there with different stripes on them to tell you the level of resistance they provide in a unit called ohms(𝞨). I’m not going to get into that kind of detail here because it isn’t necessary for Pico jobs.

Suffice it to say that if you run out of resistors and want more, you’ll be using little 330𝞨 guys.

Just one little note – yours might not be the same color, but the stripes will be. The ones I got in my kit are blue, but that doesn’t matter; 330𝞨 is 330𝞨.

Piezoelectric Buzzer

Everyone’s favorite component to play with – well, as long as you have people in your place to annoy – is the piezoelectric buzzer. Oh, boy, this one’s fun! It does exactly what its name indicates, and it does it by vibrating two metal plates together when a current is run through it. Heh, heh.

I’m an avid guitar player, and I LOVE the electric guitar the most. I’m the heaviest of metal heads! Ok, maybe not that heavy. I only clock in at 5’7” and 140lb. Sorry – if you’re not an American reader, that ain’t big. Martial arts has done me a lot of good in life. 🤣

Aaaaaaaanyway, there are at least two knobs on an electric guitar – one for volume, and one for tone.

Potentiometer

These both use potentiometers that can be used either as a variable resistor (with two legs connected) or a voltage divider (with all three wired up). In other words, one hookup controls ohms and the other controls volts. I’ll let you guess which version controls which function on the guitar.

Motion Detector

Ever tried to break into somebody’s house, but they had one of those pesky motion detectors that lit up the whole property and nearly got you busted? No? I guess that’s just… a friend… 😳

Well, you get one of those pesky… I mean… cool motion detectors, too. It’s actually called a passive infrared sensor (PIR), and it makes things happen when you wave your hand in front of it. Better than a button? You be the judge.

Screen and Display

Last but not least (I love overused, trite cliches, don’t you?), we have a screen. It’s actually called an inter-integrated circuit or I2C display. It can show all kinds of nifty stuff from text to pictures. There are some fun things to do with this baby!

Obviously, there are other components you can get like motors, current sensors, reverse-LEDs, and a whole host of things. Some of them require special drivers and such, though, so they wouldn’t be considered basic. For the purposes of this series, we will be focusing on more basic components and projects before graduating to higher-level (and more expensive) experiments.

Keep Learning! 👇

Next time, we’ll get into our first project. It’s pretty easy, but it’s fun and totally worth it. I can’t wait to get started, so I’ll talk to you soon. Until then, try some more of the MicroPython I taught you before. Happy coding!

Posted on Leave a comment

ModuleNotFoundError: No Module Named ‘gRPC’ in Python

4/5 – (1 vote)

Quick Fix: Python raises the ImportError: No module named 'grpc' when it cannot find the library grpc. The most frequent source of this error is that you haven’t installed grpc explicitly with pip install grpcio. Alternatively, you may have different Python versions on your computer, and grpc is not installed for the particular version you’re using.

In most cases, you actually want to pip install grpcio and not grpc. If you really want to install grpcio, replace all occurrences of pip install grpc (and variants) with pip install grpcio.

Here are some examples that may work depending on your environment:

python -m pip install grpcio
sudo pip install grpcio
pip.exe install grpcio

Also, make sure to install grpcio-tools:

pip install grpcio-tools

Problem Formulation

You’ve just learned about the awesome capabilities of the grpc library and you want to try it out, so you start your code with the following statement:

import grpc

This is supposed to import the gRPC library into your (virtual) environment. However, it only throws the following ImportError: No module named grpc:

>>> import grpc
Traceback (most recent call last): File "<pyshell#6>", line 1, in <module> import grpc
ModuleNotFoundError: No module named 'grpc'

By the way, this graphic shows the purpose of gRPC and what it is:

Solution Idea 1: Install Library grpc

The most likely reason is that Python doesn’t provide grpc in its standard library. You need to install it first!

Before being able to import the grpc module, you need to install it using Python’s package manager pip. Make sure pip is installed on your machine.

To fix this error, you can run the following command in your Windows shell:

$ pip install grpc

or

$ pip install grpcio

This simple command installs grpc in your virtual environment on Windows, Linux, and MacOS. It assumes that your pip version is updated. If it isn’t, use the following two commands in your terminal, command line, or shell (there’s no harm in doing it anyways):

$ python -m pip install – upgrade pip
$ pip install grpcio

💡 Note: Don’t copy and paste the $ symbol. This is just to illustrate that you run it in your shell/terminal/command line.

Solution Idea 2: Fix the Path

The error might persist even after you have installed the grpc library. This likely happens because pip is installed but doesn’t reside in the path you can use. Although pip may be installed on your system the script is unable to locate it. Therefore, it is unable to install the library using pip in the correct path.

To fix the problem with the path in Windows follow the steps given next.

Step 1: Open the folder where you installed Python by opening the command prompt and typing where python

Step 2: Once you have opened the Python folder, browse and open the Scripts folder and copy its location. Also verify that the folder contains the pip file.

Step 3: Now open the Scripts directory in the command prompt using the cd command and the location that you copied previously.

Step 4: Now install the library using pip install grpcio command. Here’s an analogous example:

After having followed the above steps, execute our script once again. And you should get the desired output.

Other Solution Ideas

  • The ModuleNotFoundError may appear due to relative imports. You can learn everything about relative imports and how to create your own module in this article.
  • You may have mixed up Python and pip versions on your machine. In this case, to install grpc for Python 3, you may want to try python3 -m pip install grpcio or even pip3 install grpcio instead of pip install grpcio
  • If you face this issue server-side, you may want to try the command pip install – user grpcio
  • If you’re using Ubuntu, you may want to try this command: sudo apt install grpcio
  • You can also check out this article to learn more about possible problems that may lead to an error when importing a library.

Understanding the “import” Statement

import grpc

In Python, the import statement serves two main purposes:

  • Search the module by its name, load it, and initialize it.
  • Define a name in the local namespace within the scope of the import statement. This local name is then used to reference the accessed module throughout the code.

What’s the Difference Between ImportError and ModuleNotFoundError?

What’s the difference between ImportError and ModuleNotFoundError?

Python defines an error hierarchy, so some error classes inherit from other error classes. In our case, the ModuleNotFoundError is a subclass of the ImportError class.

You can see this in this screenshot from the docs:

You can also check this relationship using the issubclass() built-in function:

>>> issubclass(ModuleNotFoundError, ImportError)
True

Specifically, Python raises the ModuleNotFoundError if the module (e.g., grpc) cannot be found. If it can be found, there may be a problem loading the module or some specific files within the module. In those cases, Python would raise an ImportError.

If an import statement cannot import a module, it raises an ImportError. This may occur because of a faulty installation or an invalid path. In Python 3.6 or newer, this will usually raise a ModuleNotFoundError.

Related Videos

The following video shows you how to resolve the ImportError:

YouTube Video

The following video shows you how to import a function from another folder—doing it the wrong way often results in the ModuleNotFoundError:

YouTube Video

How to Fix “ModuleNotFoundError: No module named ‘grpc’” in PyCharm

If you create a new Python project in PyCharm and try to import the grpc library, it’ll raise the following error message:

Traceback (most recent call last): File "C:/Users/.../main.py", line 1, in <module> import grpc
ModuleNotFoundError: No module named 'grpc' Process finished with exit code 1

The reason is that each PyCharm project, per default, creates a virtual environment in which you can install custom Python modules. But the virtual environment is initially empty—even if you’ve already installed grpc on your computer!

Here’s a screenshot exemplifying this for the pandas library. It’ll look similar for grpc.

The fix is simple: Use the PyCharm installation tooltips to install Pandas in your virtual environment—two clicks and you’re good to go!

First, right-click on the pandas text in your editor:

Second, click “Show Context Actions” in your context menu. In the new menu that arises, click “Install Pandas” and wait for PyCharm to finish the installation.

The code will run after your installation completes successfully.

As an alternative, you can also open the Terminal tool at the bottom and type:

$ pip install grpcio

If this doesn’t work, you may want to set the Python interpreter to another version using the following tutorial: https://www.jetbrains.com/help/pycharm/2016.1/configuring-python-interpreter-for-a-project.html

You can also manually install a new library such as grpc in PyCharm using the following procedure:

  • Open File > Settings > Project from the PyCharm menu.
  • Select your current project.
  • Click the Python Interpreter tab within your project tab.
  • Click the small + symbol to add a new library to the project.
  • Now type in the library to be installed, in your example Pandas, and click Install Package.
  • Wait for the installation to terminate and close all popup windows.

Here’s an analogous example:

Here’s a full guide on how to install a library on PyCharm.

Posted on Leave a comment

Parsing XML Files in Python – 4 Simple Ways

5/5 – (1 vote)

Problem Formulation and Solution Overview

This article will show you various ways to work with an XML file.

ℹ XML is an acronym for Extensible Markup Language. This file type is similar to HTML. However, XML does not have pre-defined tags like HTML. Instead, a coder can define their own tags to meet specific requirements. XML is a great way to transmit and share data, either locally or via the internet. This file can be parsed based on standardized XML if structured correctly.

To make it more interesting, we have the following running scenario:

Jan, a Bookstore Owner, wants to know the top three (3) selling Books in her store. This data is currently saved in an XML format.


💬 Question: How would we write code to read in and extract data from an XML file into a Python script?

We can accomplish this by performing the following steps:


Method 1: Use xmltodict()

This method uses the xmltodict() function to read an XML file, convert it to a Dictionary and extract the data.

In the current working directory, create an XML file called books.xml. Copy and paste the code snippet below into this file and save it.

<bookstore> <book> <title>Surrender</title> <author>Bono</author> <sales>21987</sales> </book> <book> <title>Going Rogue</title> <author>Janet Evanovich</author> <sales>15986</sales> </book> <book> <title>Triple Cross</title> <author>James Patterson</author> <sales>11311</sales> </book>
</bookstore>

In the current working directory, create a Python file called books.py. Copy and paste the code snippet below into this file and save it. This code reads in and parses the above XML file. If necessary, install the xmltodict library.

import xmltodict with open('books.xml', 'r') as fp: books_dict = xmltodict.parse(fp.read()) fp.close() for i in books_dict: for j in books_dict[i]: for k in books_dict[i][j]: print(f'Title: {k["title"]} \t Sales: {k["sales"]}')

The first line in the above code snippet imports the xmltodict library. This library is needed to access and parse the XML file.

The following highlighted section opens books.xml in read mode (r) and saves it as a File Object, fp. If fp was output to the terminal, an object similar to the one below would display.

<_io.TextIOWrapper name='books.xml' mode='r' encoding='cp1252'>

Next, the xmltodict.parse() function is called and passed one (1) argument, fp.read(), which reads in and parses the contents of the XML file. The results save to books_dict as a Dictionary, and the file is closed. The contents of books_dict are shown below.

{'bookstore': {'book': [{'title': Surrender', 'author': 'Bono', 'sales': '21987'}, {'title': 'Going Rogue', 'author': 'Janet Evanovich', 'sales': '15986'}, {'title': 'Triple Cross', 'author': 'James Patterson', 'sales': '11311'}]}}

The final highlighted section loops through the above Dictionary and extracts each book’s Title and Sales.

Title: Surrender Sales: 21987
Title: Going Rogue Sales: 15986
Title: Triple Cross Sales: 11311

💡 Note: The \t character represents the <Tab> key on the keyboard.

YouTube Video

Method 2: Use minidom.parse()

This method uses the minidom.parse() function to read and parse an XML file. This example extracts the ID, Title and Sales for each book.

This example differs from Method 1 as this XML file contains an additional line at the top (<?xml version="1.0"?>) of the file and each <book> tag now has an id (attribute) assigned to it.

In the current working directory, create an XML file called books2.xml. Copy and paste the code snippet below into this file and save it.

<?xml version="1.0"?>
<bookstore> <storename>Jan's Best Sellers List</storename> <book id="21237"> <title>Surrender</title> <author>Bono</author> <sales>21987</sales> </book> <book id="21946"> <title>Going Rogue</title> <author>Janet Evanovich</author> <sales>15986</sales> </book> <book id="18241"> <title>Triple Cross</title> <author>James Patterson</author> <sales>11311</sales> </book>
</bookstore>

In the current working directory, create a Python file called books2.py. Copy and paste the code snippet below into this file and save it.

from xml.dom import minidom doc = minidom.parse('books2.xml')
name = doc.getElementsByTagName('storename')[0]
books = doc.getElementsByTagName('book') for b in books: bid = b.getAttribute('id') title = b.getElementsByTagName('title')[0] sales = b.getElementsByTagName('sales')[0] print(f'{bid} {title.firstChild.data} {sales.firstChild.data}')

The first line in the above code snippet imports the minidom library. This allows access to various functions to parse the XML file and retrieve tags and attributes.

The first section of highlighted lines performs the following:

  • Reads and parse the books2.xml file and saves the results to doc. This action creates the Object shown as (1) below.
  • Retrieves the <storename> tag and saves the results to name. This action creates an Object shown as (2) below.
  • Retrieves the <book> tag for each book and saves the results to books. This action creates a List of three (3) Objects: one for each book shown as (3) below.
(1) <xml.dom.minidom.Document object at 0x0000022D764AFEE0> (2) <DOM Element: storename at 0x22d764f0ee0> (3) [<DOM Element: book at 0x22d764f3a30>, <DOM Element: book at 0x22d764f3c70>, <DOM Element: book at 0x22d764f3eb0>]

The last section of highlighted lines loop through the books Object and outputs the results to the terminal.

21237 Surrender 21987
21946 Going Rogue 15986
18241 Triple Cross 11311
YouTube Video

Method 3: Use etree

This method uses etree to read in and parses an XML file. This example extracts the Title and Sales data for each book.

ℹ The etree considers the XML file as a tree structure. Each element represents a node of said tree. Accessing elements is done on an element level.

This example reads in and parses the books2.xml file created earlier.

import xml.etree.ElementTree as ET xml_data = ET.parse('books2.xml')
root = xml_data.getroot() for books in root.findall('book'): title = books.find('title').text author = books.find('author').text sales = books.find('sales').text print(title, author, sales)

The first line in the above code snippet imports the etree library. This allows access to all nodes of the XML <tag> structure.

The following line reads in and parses books2.xml. The results save as an XML Object to xml_data. If output to the terminal, an Object similar to the one below displays.

<Element 'bookstore' at 0x000001E45E9442C0>

The following highlighted section uses a for loop to iterate through each <book> tag, extracting the <title>, <author> and <sales> tags for each book and outputting them to the terminal.

Surrender Bono 21987
Going Rogue Janet Evanovich 15986
Triple Cross James Patterson 11311

To retrieve the attribute of the <book> tag, run the following code.

This code extracts the id attribute from each <book> tag and outputs it to the terminal.

{'id': '21237'}
{'id': '21946'}
{'id': '18241'}

To extract the values, run the following code.

for id in root.iter('book'): vals = id.attrib.values() for v in vals: print(vals)
21237
21946
18241

Method 4: Use untangle.parse()

This method uses untangle.parse() to parse an XML string.

This example reads in and parses the books3.xml file shown below. If necessary, install the untangle library.

ℹ The untangle library converts an XML file to a Python object. This is a good option when you have a group of items, such as book names.

In the current working directory, create an XML file called books3.xml. Copy and paste the code snippet below into this file and save it. If necessary, install the untangle library.

<?xml version="1.0"?>
<root> <book name="Surrender"/> <book name="Going Rogue"/> <book name="Triple Cross"/>
</root>

In the current working directory, create a Python file called books3.py. Copy and paste the code snippet below into this file and save it.

import untangle book_obj = untangle.parse('books3.xml')
books = ','.join([book['name'] for book in book_obj.root.book]) for b in books.split(','): print(b)

The first line in the above code snippet imports the untangle library allowing access to the XML file structure.

The following line reads in and parses the books3.xml file. The results save to book_obj.

The next line calls the join() function and passes it one (1) argument: List Comprehension. This code iterates through and retrieves the name of each book and saves the results to books. If output to the terminal, the following displays:

 Surrender,Going Rogue,Triple Cross

The next line instantiates a for loop, iterates through each book name, and sends it to the terminal.

Surrender
Going Rogue
Triple Cross
YouTube Video

Summary

This article has shown four (4) ways to work with XML files to select the best fit for your coding requirements.

Good Luck & Happy Coding!


Programmer Humor – Blockchain

“Blockchains are like grappling hooks, in that it’s extremely cool when you encounter a problem for which they’re the right solution, but it happens way too rarely in real life.” source xkcd
Posted on Leave a comment

How to Schedule a Batch Python Script

5/5 – (1 vote)

Problem Formulation and Solution Overview

During your career as a Pythonista, you will encounter situations where a Python script will need to be executed on a scheduled basis, such as daily, weekly, or monthly.

This article shows you how to accomplish this task using a .bat (batch) file.


💬 Question: How would we write code to run a .bat (batch) file on a schedule?

We can accomplish this task by completing the following steps:

  1. Create a Python Script
  2. Create a .bat File
  3. Execute a .bat File
  4. Schedule a .bat File Using Windows Task Scheduler
  5. Bonus: Schedule a Monthly .bat File

Create a Python Script

Let’s first start by creating a Python script that counts down from five (5) to one (1).

In the current working directory, create a Python file called counter.py. Copy and paste the code snippet below into this file and save it.

from time import sleep lift_off = 5 while lift_off > 0: print (f'Lift Off in {lift_off} seconds!') sleep(2) lift_off -= 1

The first line in the above code snippet imports the time library. This allows access to the sleep() function, which pauses the script between iterations.

Next, a while loop is instantiated and executes the code inside this loop until the value of lift_off is zero (0).

On each iteration, the following occurs:

  • A line of text is output to the terminal indicating the value of lift_off.
  • The script pauses for two (2) seconds.
  • The value of lift_off is decreased by one (1).

To confirm script runs successfully. Navigate to the command prompt and run the following:

python counter.py

The output from this script should be as follows:

Lift Off in 5 seconds!
Lift Off in 4 seconds!
Lift Off in 3 seconds!
Lift Off in 2 seconds!
Lift Off in 1 seconds!

Great! Now let’s create a .bat (Batch) file to run this script!

YouTube Video

Create a .bat File

This section creates a .bat file that executes counter.py by calling this file inside the .bat file.

In the current working directory, create a Python file called counter.bat. Copy and paste the code snippet below into this file and save it.

@echo off "C:\Python\python.exe" "C:\PYTHON_CODE\counter.py"

The first line of the code snippet turns off any output to the terminal (except the code inside counter.py). For example, If the first line (@echo off) was removed and counter.bat was executed, the following would be output to the terminal.

C:\WORK> "C:\Python\python.exe" "C:\PYTHON_CODE\counter.py"
Lift Off in 5 seconds!
Lift Off in 4 seconds!
Lift Off in 3 seconds!
Lift Off in 2 seconds!
Lift Off in 1 seconds!

The following line of code specifies the following:

  • The location of the python.exe file on your computer.
  • The location of the python script to execute.

Let’s see if this works!

💡 Note: It is best practice to ensure that the full paths to the python.exe and counter.py files are added.


Execute a .bat File

This section executes the .bat file created earlier. This code calls and executes the code inside the counter.py file.

To run the .bat file, navigate to the IDE, and click to select and highlight the counter.bat file. Then, press the F5 key on the keyboard to execute.

If successful, the output should be the same as running the counter.py file directly.

Lift Off in 5 seconds!
Lift Off in 4 seconds!
Lift Off in 3 seconds!
Lift Off in 2 seconds!
Lift Off in 1 seconds!

Perfect! Let’s schedule this to run Daily at a specified time.


Schedule a .bat File Using Windows Task Scheduler

This example uses Windows Task Scheduler to schedule a .bat file to run at a specified date/time.

To set up a Task Scheduler on Windows, navigate to the command prompt from Windows and run the following code:

taskschd.msc

Alternatively, click the Windows start button, search for, and select Task Scheduler.

Either of the above actions will display the Task Scheduler pop-up.

From the Actions area, click Create Basic Task. This action displays the Create a Basic Task Wizard pop-up.

From the Create a Basic Task pop-up, enter a Name and Description into the appropriate text boxes. Click the Next button to continue.

This action displays the Task Trigger pop-up. Select when to run the .bat file. For this example, Daily was chosen. Click the Next button to continue.

Since Daily was selected earlier, the Daily pop-up displays. Modify the fields to meet the desired date and time requirements. Click the Next button to continue.

This action displays the Action pop-up. Select Start a program. Click the Next button to continue.

This action displays the Start a Program pop-up. Browse to select the counter.bat file created earlier. Click the Next button to continue.

This action displays the Summary pop-up. If satisfied with the selections made earlier, click the Finish button to complete the setup.

Great! The task is now scheduled to run at the date/time specified.


View, Edit, or Delete a Scheduled Task

To view a list of Scheduled Tasks, navigate to the Task Scheduler pop-up and select Task Scheduler Library.

To delete a task, click to select the appropriate task from the list of scheduled events. Then click the Delete link on the right-hand side.

To edit a task, click to select the appropriate task from the list of scheduled events. Then click the Properties link on the right-hand side to display the Properties pop-up. From this pop-up, all of the above selections can be modified.

Click the OK button to confirm any changes and close the pop-up.

💡 Note: We recommend you review the fields on each tab to learn more about scheduling tasks.


Bonus: Schedule a Monthly .bat File

This section reads in a CSV containing sales data. This data is then sorted and filtered based on the current month. This is scheduled to run on the first day of each month. To follow along, download the CSV file.

In the current working directory, create a Python file called sales.py. Copy and paste the code snippet below into this file and save it.

import pandas as pd from datetime import datetime
import openpyxl today = datetime.now()
cols = ['OrderDate', 'Region', 'Item', 'Units'] df = pd.read_csv('sales.csv', usecols=cols)
df["OrderDate"] = pd.to_datetime(df["OrderDate"])
df = df.sort_values(by=['OrderDate']) df_monthly = df[df['OrderDate'].dt.month == today.month]
df_monthly.to_excel('monthly_rpt.xlsx', columns=cols, index=False, header=True)

In the current working directory, create a Python file called sales.bat Copy and paste the code snippet below into this file and save it. Modify to meet your locations.

@echo off "C:\Python\python.exe" "C:\PYTHON_CODE\sales.py"

Let’s set up a Monthly schedule to run on the first day of each month by performing the following steps:

  • Start the Windows Task Scheduler.
  • From the Task Scheduler pop-up, select Create Basic Task from the Actions area.
  • From the Create a Basic Task pop-up, enter a Name and Description in the appropriate text boxes. Click the Next button to continue.
  • From the Task Trigger pop-up, select Monthly. Click the Next button to continue.
  • From the Monthly pop-up, complete the fields as outlined below:
    • A Start Date and Start Time.
    • From the Months dropdown, select each month that the report will run. For this example, all months were selected.
    • From the Days dropdown, select the day(s) of the month to run this report. For this example, 1 was selected.
    • Click the Next button to continue.
  • From the Action pop-up, select Start a Program. Click the Next button to continue.
  • From the Start a Program pop-up, click the Browse button to locate and select the sales.bat file.
  • From the Summary window click the Finish button.

This completes the configuration and activates the Scheduler to run on the specified day/time.


Summary

This article has shown you have to create and run a .bat file that executes a Python script on a scheduled basis.

Good Luck & Happy Coding!


Programming Humor – Python

“I wrote 20 short programs in Python yesterday. It was wonderful. Perl, I’m leaving you.”xkcd

Posted on Leave a comment

[Fixed] Python ModuleNotFoundError: No Module Named ‘readline’

5/5 – (1 vote)

Quick Fix: Python raises the ImportError: No module named 'readline' when it cannot find the library readline. The most frequent source of this error is that you haven’t installed readline explicitly with pip install readline. Alternatively, you may have different Python versions on your computer, and readline is not installed for the particular version you’re using.

pip install readline

Library Link: https://pypi.org/project/readline/

⚡ Attention: This module is depreciated, you may want to install gnureadline instead:

pip install gnureadline

Problem Formulation

You’ve just learned about the awesome capabilities of the readline library and you want to try it out, so you start your code with the following statement:

import readline

This is supposed to import the readline library into your (virtual) environment. However, it only throws the following ImportError: No module named readline:

>>> import readline
Traceback (most recent call last): File "<pyshell#6>", line 1, in <module> import readline
ModuleNotFoundError: No module named 'readline'

Solution Idea 1: Install Library readline

The most likely reason is that Python doesn’t provide readline in its standard library. You need to install it first!

Before being able to import the readline module, you need to install it using Python’s package manager pip. Make sure pip is installed on your machine.

To fix this error, you can run the following command in your Windows shell:

$ pip install readline

This simple command installs readline in your virtual environment on Windows, Linux, and MacOS. It assumes that your pip version is updated. If it isn’t, use the following two commands in your terminal, command line, or shell (there’s no harm in doing it anyways):

$ python -m pip install – upgrade pip
$ pip install readline

💡 Note: Don’t copy and paste the $ symbol. This is just to illustrate that you run it in your shell/terminal/command line.

Solution Idea 2: Fix the Path

The error might persist even after you have installed the readline library. This likely happens because pip is installed but doesn’t reside in the path you can use. Although pip may be installed on your system the script is unable to locate it. Therefore, it is unable to install the library using pip in the correct path.

To fix the problem with the path in Windows follow the steps given next.

Step 1: Open the folder where you installed Python by opening the command prompt and typing where python

Step 2: Once you have opened the Python folder, browse and open the Scripts folder and copy its location. Also verify that the folder contains the pip file.

Step 3: Now open the Scripts directory in the command prompt using the cd command and the location that you copied previously.

Step 4: Now install the library using pip install readline command. Here’s an analogous example:

After having followed the above steps, execute our script once again. And you should get the desired output.

Other Solution Ideas

  • The ModuleNotFoundError may appear due to relative imports. You can learn everything about relative imports and how to create your own module in this article.
  • You may have mixed up Python and pip versions on your machine. In this case, to install readline for Python 3, you may want to try python3 -m pip install readline or even pip3 install readline instead of pip install readline
  • If you face this issue server-side, you may want to try the command pip install – user readline
  • If you’re using Ubuntu, you may want to try this command: sudo apt install readline
  • You can check out our in-depth guide on installing readline here.
  • You can also check out this article to learn more about possible problems that may lead to an error when importing a library.

Understanding the “import” Statement

import readline

In Python, the import statement serves two main purposes:

  • Search the module by its name, load it, and initialize it.
  • Define a name in the local namespace within the scope of the import statement. This local name is then used to reference the accessed module throughout the code.

What’s the Difference Between ImportError and ModuleNotFoundError?

What’s the difference between ImportError and ModuleNotFoundError?

Python defines an error hierarchy, so some error classes inherit from other error classes. In our case, the ModuleNotFoundError is a subclass of the ImportError class.

You can see this in this screenshot from the docs:

You can also check this relationship using the issubclass() built-in function:

>>> issubclass(ModuleNotFoundError, ImportError)
True

Specifically, Python raises the ModuleNotFoundError if the module (e.g., readline) cannot be found. If it can be found, there may be a problem loading the module or some specific files within the module. In those cases, Python would raise an ImportError.

If an import statement cannot import a module, it raises an ImportError. This may occur because of a faulty installation or an invalid path. In Python 3.6 or newer, this will usually raise a ModuleNotFoundError.

Related Videos

The following video shows you how to resolve the ImportError:

YouTube Video

The following video shows you how to import a function from another folder—doing it the wrong way often results in the ModuleNotFoundError:

YouTube Video

How to Fix “ModuleNotFoundError: No module named ‘readline’” in PyCharm

If you create a new Python project in PyCharm and try to import the readline library, it’ll raise the following error message:

Traceback (most recent call last): File "C:/Users/.../main.py", line 1, in <module> import readline
ModuleNotFoundError: No module named 'readline' Process finished with exit code 1

The reason is that each PyCharm project, per default, creates a virtual environment in which you can install custom Python modules. But the virtual environment is initially empty—even if you’ve already installed readline on your computer!

Here’s a screenshot exemplifying this for the pandas library. It’ll look similar for readline.

The fix is simple: Use the PyCharm installation tooltips to install Pandas in your virtual environment—two clicks and you’re good to go!

First, right-click on the pandas text in your editor:

Second, click “Show Context Actions” in your context menu. In the new menu that arises, click “Install Pandas” and wait for PyCharm to finish the installation.

The code will run after your installation completes successfully.

As an alternative, you can also open the Terminal tool at the bottom and type:

$ pip install readline

If this doesn’t work, you may want to set the Python interpreter to another version using the following tutorial: https://www.jetbrains.com/help/pycharm/2016.1/configuring-python-interpreter-for-a-project.html

You can also manually install a new library such as readline in PyCharm using the following procedure:

  • Open File > Settings > Project from the PyCharm menu.
  • Select your current project.
  • Click the Python Interpreter tab within your project tab.
  • Click the small + symbol to add a new library to the project.
  • Now type in the library to be installed, in your example Pandas, and click Install Package.
  • Wait for the installation to terminate and close all popup windows.

Here’s an analogous example:

Here’s a full guide on how to install a library on PyCharm.

Posted on Leave a comment

Convert JSON String to JavaScript Object

by Vincy. Last modified on November 10th, 2022.

The JSON string is a convenient format to transfer data between terminals. Almost all of the API responses you see are in JSON string format.

The JSON string should be parsed to read the data bundled with this string.

JSON.parse function is used to convert a JSON string into a JavaScript object.

Quick example

The below quick example has an input JSON string having properties of animals. The properties are stored in a multi-level hierarchy.

The JSON.parse() JS function converts this JSON string input into an object array.

const jsonString = `{ "animals": { "Lion": { "name": "Lion", "type": "Wild", "Location": { "1": { "zoo-1": "San Diego Zoo", "zoo-2": "Bronx Zoo" } } } }
}`; javaScriptObject = JSON.parse(jsonString)
console.log(javaScriptObject);

The above code will log the output of the converted JSON into the browser’s developer console.

Output:

animals: Lion: Location: 1: zoo-1: "San Diego Zoo" zoo-2: "Bronx Zoo" name: "Lion" type: "Wild"

json string to javascript object

The source JSON string can be from many different resources. For example,

  1. It can be stored in the database that is to be parsed.
  2. It can be a response to an API.

The JSON string will contain many types of data like dates, functions and more.

The following examples give code to learn how to convert a JSON string that contains different types of data. In the previous article, we have seen how to convert a JavaScript object to JSON.

How the date in the JSON string will be converted

If the input JSON string contains date values, the JSON.parse() JS function results in a date string.

For example, the date in 2014-11-25 into Tue Nov 25 2014 05:30:00 GMT+0530.

It will be converted later into a JavaScript date object.

// JSON string with date to JavaScript object
const jsonString = '{"animal":"Lion", "birthdate":"2014-11-25", "zoo":"Bronx Zoo"}';
const jsObject = JSON.parse(jsonString);
jsObject.birthdate = new Date(jsObject.birthdate);
console.log(jsObject.birthdate);

Output:

Tue Nov 25 2014 05:30:00 GMT+0530

JSON input script with function to JavaScript object

If a JSON script contains a function as its value, the below code shows how to parse the input JSON. In an earlier tutorial, we have see many functions of JSON handling using PHP.

It applied JSON.parse as usual and get the scope of the function by using JavaScript eval(). The JavaScript object index gets the scope to access the function in the input JSON string.

Note: Using the JS eval() is a bad idea if you are working with sensitive data. So avoid using functions as a string inside a JS JSON input.

// JSON string with a function to JavaScript object and invoke the function
const jsonString = '{"animal":"Lion", "birthdate":"2014-11-25", "id":"function () {return 101;}"}';
const jsObject = JSON.parse(jsonString);
jsObject.id = eval("(" + jsObject.id + ")");
console.log(jsObject.id());
// also be aware that, when you pass functions in JSON it will lose the scope

JSON.parse reviver to convert the date string to a JavaScript object

In a previous example, we parsed the date as a string and then converted it into a Date object.

Instead of converting the resultant Date string into a Date object later, this program uses JSON.parse() with its reviver parameter.

The reviver parameter is a callback function created below to convert the input date into a Date object.

Using this method, the output Javascript object will include the date object as converted by the reviver method.

// JSON string with a date to JavaScript object using the reviver parameter of JSON.parse
const jsonString = '{"animal": "Lion", "birthdate": "2014-11-25", "zoo": "Bronx Zoo"}';
const jsObject = JSON.parse(jsonString, function(key, value) { if (key == "birthdate") { return new Date(value); } else { return value; }
}); jsObject.birthdate = new Date(jsObject.birthdate);
console.log(jsObject.birthdate);

The reviver parameter is optional while using the JSOM.parse function. The callback defined as a reviver will check each item of the input JSON string.

↑ Back to Top

Posted on Leave a comment

Python | Split String and Keep Newline

Rate this post

Summary: Use 'given_string'.splitlines(True) to split the string and also keep the new line character.

Minimal Example:

text = 'abc\nlmn\nxyz'
print(text.splitlines(True)) # OUTPUT: ['abc\n', 'lmn\n', 'xyz']

Problem Formulation

📜Problem: Given a string. How will you split the string into a list of substrings and keep the new line character intact?

Example: Let’s have a look at a test case to understand the given problem.

# Input
text = """Sun
Earth
Moon""" # Expected Output:
['Sun\n', 'Earth\n', 'Moon']
OR
['Sun', '\n', 'Earth', '\n', 'Moon']

Without further ado, let us now dive into the different solutions for the given problem.

Method 1: Use splitlines(True)

Approach: The splitlines() method is used to split the string at all line breaks. If you pass True as a parameter within the splitlines method, then the resultant list includes the newline character along with the substring/item.

Code:

text = """Sun
Earth
Moon"""
print(text.splitlines(True)) # OUTPUT: ['Sun\n', 'Earth\n', 'Moon']

🌎Related Tutorial: Python String splitlines()

Method 2: Use regex

The re.split(pattern, string) method matches all occurrences of the pattern in the string and divides the string along the matches resulting in a list of strings between the matches. For example, re.split('a', 'bbabbbab') results in the list of strings ['bb', 'bbb', 'b']. Read more here – Python Regex Split.

Approach: Use re.split('(\W)', 'given_string') where the brackets() ensure the separators/delimiters are also stored in the list along with the word characters and \W is a special sequence that returns a match where it does not find any word characters in the given string. Here it is used to find the delimiters while splitting the string.

Code:

import re
text = """Sun
Earth
Moon"""
print(re.split('(\W)', text)) # OUTPUT: ['Sun', '\n', 'Earth', '\n', 'Moon']

Note: Instead of “\W” you are free to use any other expression that suits your needs however, make sure to enclose it within brackets to ensure that the newline characters (delimiter) are also included.

In case you do not want to include the separators as independent items, instead, you want to include them along with the split substrings/items, then you can simply split the given string using “\n” as the separator and then append or concatenate the newline character to each substring/item one by one except the last item. This is what you can do:-

import re
text = """Sun
Earth
Moon"""
res = re.split('\n', text)
output = []
for i in range(len(res)-1): output.append(res[i]+"\n")
output.append(res[-1])
print(output) # Alternate Formulation
res = [x+"\n" for x in re.split('\n', text)]
res[-1] = res[-1].strip('\n')
print(res) # OUTPUT: ['Sun\n', 'Earth\n', 'Moon']

Do you want to master the regex superpower? Check out my new book The Smartest Way to Learn Regular Expressions in Python with the innovative 3-step approach for active learning: (1) study a book chapter, (2) solve a code puzzle, and (3) watch an educational chapter video.

Method 3: Using a List Comprehension 

Approach: Use a list comprehension to split the given string using a for loop and the split() method and return each substring as an item and concatenate the separator (“new line character” in this case) along with the item. Note that the resultant list will have an extra “\n” character at the end. You can simply strip this new line character from the last element of the list.

Code:

text = """Sun
Earth
Moon"""
# split string and keep "\n"
res = [x+"\n" for x in text.split()]
# remove the extra "\n" character from the last item of the list res[-1] = res[-1].strip('\n')
print(res) # OUTPUT: ['Sun\n', 'Earth\n', 'Moon']

If you want the separator as an independent item in the list then go for the following expression –

text = """Sun
Earth
Moon"""
res = [u for x in text.split('\n') for u in (x, '\n')]
res.pop(-1)
print(res) # OUTPUT: ['Sun', '\n', 'Earth', '\n', 'Moon']

Conclusion

We have successfully solved the given problem using different approaches. I hope this article helped you in your Python coding journey. Please subscribe and stay tuned for more interesting articles.

Happy Pythoning! 🐍 

Related Reads:
⦿ Python | Split String by Newline
⦿ How To Split A String And Keep The Separators?
⦿ Python | Split String into Characters


But before we move on, I’m excited to present you my new Python book Python One-Liners (Amazon Link).

If you like one-liners, you’ll LOVE the book. It’ll teach you everything there is to know about a single line of Python code. But it’s also an introduction to computer science, data science, machine learning, and algorithms. The universe in a single line of Python!

The book was released in 2020 with the world-class programming book publisher NoStarch Press (San Francisco).

Link: https://nostarch.com/pythononeliners