Categories
Python Answers

How to print a single backslash with Python?

Sometimes, we want to print a single backslash with Python.

In this article, we’ll look at how to print a single backslash with Python.

How to print a single backslash with Python?

To print a single backslash with Python, we pass in '\\' into print.

For instance, we write:

print("\\")

And we see \ printed.

Conclusion

To print a single backslash with Python, we pass in '\\' into print.

Categories
Python Answers

How to create a cartesian product with Python Pandas?

Sometimes, we want to create a cartesian product with Python Pandas.

In this article, we’ll look at how to create a cartesian product with Python Pandas.

How to create a cartesian product with Python Pandas?

To create a cartesian product with Python Pandas, we can call the merge method.

For instance, we write:

from pandas import DataFrame

df1 = DataFrame({'col1': [1, 2], 'col2': [3, 4]})
df2 = DataFrame({'col3': [5, 6]})

df = df1.merge(df2, how='cross')
print(df)

to call df1.merge to merge data frtame df1 with df2.

We set how to 'cross' to create a cartesian product and assign the returned data frame to df.

Therefore, df is:

   col1  col2  col3
0     1     3     5
1     1     3     6
2     2     4     5
3     2     4     6

Conclusion

To create a cartesian product with Python Pandas, we can call the merge method.

Categories
Python Answers

How to convert hex string to int in Python?

Sometimes, we want to convert hex string to int in Python.

In this article, we’ll look at how to convert hex string to int in Python.

How to convert hex string to int in Python?

To convert hex string to int in Python, we can call the int function.

For instance, we write:

x = int("deadbeef", 16)
y = int("0xdeadbeef", 0)
print(x)
print(y)

We call int with the hex number with or without the 0x prefix.

If we skip the 0x prefix, then we need to pass in the base as the 2nd argument.

Otherwise, we can pass in 0 for the 2nd argument.

Therefore, x and y are both 3735928559.

Conclusion

To convert hex string to int in Python, we can call the int function.

Categories
Python Answers

How to save an object to disk with Python?

Sometimes, we want to save an object to disk with Python.

In this article, we’ll look at how to save an object to disk with Python.

How to save an object to disk with Python?

To save an object to disk with Python, we can use the pickle module.

For instance, we write:

import pickle


class Company(object):
    def __init__(self, name, value):
        self.name = name
        self.value = value


company = Company('foo', 'bar')


def save_object(obj, filename):
    with open(filename, 'wb') as outp:
        pickle.dump(obj, outp, pickle.HIGHEST_PROTOCOL)


save_object(company, 'company.pkl')

We have the Company class that we instantiated and assigned to company.

Then we define the save_object function that opens filename and call pickle.dump with obj, the outp file and pickle.HIGHEST_PROTOCOL to always save the file with the latest data.

Conclusion

To save an object to disk with Python, we can use the pickle module.

Categories
Python Answers

How to write JSON data to a file with Python?

Sometimes, we want to write JSON data to a file with Python.

In this article, we’ll look at how to write JSON data to a file with Python.

How to write JSON data to a file with Python?

To write JSON data to a file with Python, we can use the open function and the json.dump method.

For instance, we write:

import json

data = {'foo': 1, 'bar': 2}
with open('data.json', 'w', encoding='utf-8') as f:
    json.dump(data, f, ensure_ascii=False, indent=2)

to define the data dictionary which we want to write into the JSON file.

Then we call open with 'data.json' to open the data.json file.

'w' lets us open the file with write permission.

encoding sets the text encoding of the file.

Then we call json.dump with data and f to write data to file f.

ensure_ascii set to False to skip ASCII check in the file.

indent is set to 2 to indent each level with 2 spaces.

Conclusion

To write JSON data to a file with Python, we can use the open function and the json.dump method.