Pip | XML to JSON | file splitter for uploading *

right and wrong way to install python on mac

https://opensource.com/article/19/5/python-3-default-mac

[GCC 4.2.1 (Apple Inc. build 5646)] is the version of GCC that the Python(s) were built with, not the version of Python itself. That information should be on the previous line. For example:


# Apple-supplied Python 2.6 in OS X 10.6
$ /usr/bin/python
Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49) 
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 

# python.org Python 2.7.2 (also built with newer gcc)
$ /usr/local/bin/python
Python 2.7.2 (v2.7.2:8527427914a2, Jun 11 2011, 15:22:34) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

Items in /usr/bin should always be or link to files supplied by Apple in OS X, unless someone has been ill-advisedly changing things there. To see exactly where the /usr/local/bin/python is linked to:

$ ls -l /usr/local/bin/python
lrwxr-xr-x 1 root wheel 68 Jul 5 10:05 /usr/local/bin/python@ -> ../../../Libra

https://www.geeksforgeeks.org/how-to-install-pip-in-macos/

Before we start with how to install pip for Python on macOS, let’s first go through the basic introduction to Python. Python is a widely-used general-purpose, high-level programming language. Python is a programming language that lets you work quickly and integrate systems more efficiently.

PIP is a package management system used to install and manage software packages/libraries written in Python. These files are stored in a large “on-line repository” termed as Python Package Index (PyPI).

pip uses PyPI as the default source for packages and their dependencies. So whenever you type:

pip install package_name

pip will look for that package on PyPI and if found, it will download and install the package on your local system.

Download and Install pip:

pip can be downloaded and installed using command-line by going through the following steps:

 

 

  • Download the

    get-pip.py

    file and store it in the same directory as python is installed.

    or

    Use the following command to download pip directly,

    curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
    
  • Now execute the downloaded file using below command

    python3 get-pip.py
    

    and wait through the installation process.

  • Voila! pip is now installed on your system.

Verification of the Installation process:

One can easily verify if the pip has been installed correctly by performing a version check on the same. Just go to the command line and execute the following command:

pip3 --version

Attention geek! Strengthen your foundations with the Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

 

 

 

 

Splittling large xml files

 

Splitting XML file on Mac

To split files on your Mac, you need to use a different and simpler script written in python called ‘mainSplit’.

Now go to the intro page and at the botton of the page, download mainSplit.py script. Make sure to download the file in (.py) extension and not (.txt) extension.

Once downloaded, open Terminal console that’s available from Application > Utilities folder on your Mac and enter the following line:

python mainScript.py

Running the line will open a small window of ‘WordPress XML Splitter’. Click on button ‘Browse File’ to select the XML file you want to split, and then select the folder you want the file to be saved to by clicking on ‘Browse Dir.’

When ready, click the ‘Start’ button and the processing will start immediately.

To check the split files, go to the directory you have selected earlier, and you will see a collection of files, not more than 2Mb in size each.

Conclusion

Both solutions on Mac and Windows are pretty much similar, although you are using different programs and scripts. The process is simple and fast, making your migration process easy with WordPress. There are many other ways to backup your WordPress files, but this approach of splitting XML files manually does not take a lot of your time.

 

 

Converting XML to JSON

 

How to Convert XML to JSON on the Command Line

XML is complicated, and parsing it natively is fairly hard, even with scripting languages. Luckily, there’s a utility that can convert XML to JSON, which is easier to work with, both in scripts and on the command line.

Use the xq Utility

You’ll want to use a custom made utility for this, rather than trying to parse it with something like regex, which is a bad idea. There’s a utility called xq that is perfect for this task. It’s installed alongside yq, which works for YAML. You can install yq from pip:

pip install yq

Under the hood, this utility uses to handle working with JSON, so you’ll need to download the binary, and move it to somewhere on your PATH (/usr/local/bin/ should work fine).

Now, you’ll be able to parse XML input by piping it into xq:

cat xml | xq .

The . operator means you want to convert all of the XML to JSON. You can actually use full jq syntax here to select sub-elements, which you can read our guide on.

You can also output xq‘s response as XML with the -x flag:

xq -x

This lets you use jq‘s selection syntax to parse XML while keeping it in XML form. Though it doesn’t seem to be able to convert the other way, as xq still wants XML syntax in.

One Problem With XML to JSON Conversion

XML to JSON isn’t a perfect conversion—in XML, the order of the elements can matter, and keys can be duplicated. A document like:

<e>
  <a>some</a>
  <b>textual</b>
  <a>content</a>
</e>

Would produce an error if translated outright to JSON, because the a key exists twice. So, it’s converted to an array, which does break the order. xq returns the following output for that bit of XML:

{
  "e": {
    "a": [
      "some",
      "content"
    ],
    "b": "textual"
  }
}

Which is technically correct, just not ideal in all situations. You’ll want to double-check and make sure your XML conversion has no issues.

 

Scroll to Top