Categories
Python Answers

How to open multiple files using “with open” in Python?

Sometimes, we want to open multiple files using "with open" in Python.

In this article, we’ll look at how to open multiple files using "with open" in Python.

How to open multiple files using "with open" in Python?

To open multiple files using "with open" in Python, we can separate each file with a comma.

For instance, we write:

with open('foo.txt', 'r') as a, open('bar.txt', 'r') as b:
    print(a.readlines())
    print(b.readlines())

We call open with 'r' to open foo.txt and bar.txt.

Then we call readlines on each file to read the content of them.

Therefore, is foo.txt has:

foo

And bar.txt has:

bar

Then we see:

['foo']
['bar']

printed.

Conclusion

To open multiple files using "with open" in Python, we can separate each file with a comma.

Categories
Python Answers

How to make a copy of dicts on assignment with Python?

Sometimes, we want to make a copy of dicts on assignment with Python.

In this article, we’ll look at how to make a copy of dicts on assignment with Python.

How to make a copy of dicts on assignment with Python?

To make a copy of dicts on assignment with Python, we can call the copy method.

For instance, we write:

a = {'foo': 2}
b = a.copy()
b['bar'] = 3

print(a)
print(b)

We call a.copy to make a copy of a and assign it to b.

Then we added a new entry with key 'bar' to b.

Since we made a copy of a and assigned that to b, a is unchanged when we changed b.

So a is {'foo': 2} and b is {'foo': 2, 'bar': 3}.

Conclusion

To make a copy of dicts on assignment with Python, we can call the copy method.

Categories
Python Answers

How to use quotation marks inside quotation marks with Python?

Sometimes, we want to use quotation marks inside quotation marks with Python.

In this article, we’ll look at how to use quotation marks inside quotation marks with Python.

How to use quotation marks inside quotation marks with Python?

To use quotation marks inside quotation marks with Python, we can wrap single quotes around double quotes and vice versa, or we can escape single quotes in single quotes or escape double quotes in double quotes.

We can also use quotes within triple quoted strings.

For instance, we write:

print('"A word that needs quotation marks"')
print("'A word that needs quotation marks'")
print("\"A word that needs quotation marks\"")
print('\'A word that needs quotation marks\'')
print("""\"A word that needs quotation marks\"""")

In the first 2 strings, we wrap single quotes around double quotes and vice versa.

We escaped the quotes in the last 3 strings.

Therefore, we should see:

"A word that needs quotation marks"
'A word that needs quotation marks'
"A word that needs quotation marks"
'A word that needs quotation marks'
"A word that needs quotation marks"

printed.

Conclusion

To use quotation marks inside quotation marks with Python, we can wrap single quotes around double quotes and vice versa, or we can escape single quotes in single quotes or escape double quotes in double quotes.

Categories
Python Answers

How to extract first item of each sublist with Python?

Sometimes, we want to extract first item of each sublist with Python.

In this article, we’ll look at how to extract first item of each sublist with Python.

How to extract first item of each sublist with Python?

To extract first item of each sublist with Python, we can use list comprehension.

For instance, we write:

lst = [['a', 'b', 'c'], [1, 2, 3], ['x', 'y', 'z']]
lst2 = [item[0] for item in lst]
print(lst2)

to get the first item from each list in lst by getting item[0] from each item, which is each list in lst.

Therefore, lst2 is ['a', 1, 'x'].

Conclusion

To extract first item of each sublist with Python, we can use list comprehension.

Categories
Python Answers

How to download a picture via urllib and Python?

Sometimes, we want to download a picture via urllib and Python.

In this article, we’ll look at how to download a picture via urllib and Python.

How to download a picture via urllib and Python?

To download a picture via urllib and Python, we can use the urllib.request.urlretrieve method.

For instance, we write:

import urllib.request

urllib.request.urlretrieve(
    "https://i.picsum.photos/id/1055/200/300.jpg?hmac=RkWuwIAp7D4g_2_g01yQSz2pdKBzYeFDEjq86_12OHg",
    "pic.jpg")

to call urllib.request.urlretrieve with the picture URL and the file path to save the picture to.

A GET request will then be made to download the image.

Conclusion

To download a picture via urllib and Python, we can use the urllib.request.urlretrieve method.