[PYTHON] Learning record (4th day) #How to get the absolute path from the relative path

content of study

How to get an absolute path from a relative path

When the link destination is described as a relative path in HTML, use ʻurllib.parse.urljon () `to get the absolute path.

Description example
from urllib.parse import urljoin

base = "http://exsample.com/html/a.html"

compurl = lambda q: print(urljoin(base,q))
compurl("b.html")
compurl("sub/c.html")
compurl("../index.html")
compurl("../img/hoge.png ")

Execution result

http://example.com/html/b.html http://example.com/html/sub/c.html http://example.com/index.html http://example.com/img/hoge.png

Python basic grammar

pass statement

In Python, processing blocks are defined by indentation, so when there is no need to perform processing, the processing block itself disappears. Therefore, the pass statement is used to explicitly describe that nothing is to be done.

with open('exsample.txt', 'w'):
    pass

Conditional branch

Describe using ʻif, ʻelif, ʻelse`. In addition, when branching into two processing blocks by one conditional expression, it can be described using a ternary operator.

 if conditional expression 1:
 <Processing block 1>
 #Processing executed when conditional expression 1 is True
 elif conditional expression 2:
 <Processing block 2>
 #Processing executed when conditional expression 1 is False and conditional expression 2 is True
else:
 <Processing block 3>
 #Processing executed when conditional expression 1 is False, conditional expression 2 is False, and conditional expression 3 is True.

# Conditional branching by ternary operator
# Value 1 when the conditional expression is True, value 2 when False
 Value 1 if conditional expression else value 2

Iterator

Hold the data as a rule that can generate a series of data, not the value itself that represents the data. When a large amount of data is required, iterators are more memory efficient. A description example of a range type iterator is shown below.

# The following three range () return iterators increasing by 1 from 0 to 9.
range(0, 10, 1)
range(0, 10)
a = range(10)

print(a)
print(a[0])

Execution result

range(0, 10) 0

range () has the function of the increment operator (++) which cannot be used in Python. Decrement (--) is done using an iterator called reversed ().

break and continue statements

By writing in the processing block of the loop statement, the running loop statement can be controlled.

break statement

Break the currently running loop and exit the loop.

b = 0
while True:
    b += 1
    if b > 5
 When break # b = 6, the loop is exited and the process ends.
    print(b)

Execution result

1 2 3 4 5

continue statement

Suspends the processing block being executed and moves the processing to the conditional expression evaluation of the loop.

c = 0
while True:
    c += 1
    if c < 5
 The subsequent processing will not be executed until continue # b = 6.
    print(c)
    break

Execution result

6

Summary

I started having a hard time understanding the code in the scraping reference book I was using, so I decided to start learning the basic grammar of Python again. I was surprised that increment decrement, which was very convenient in C and Java, cannot be used, but it can be handled well by using cumulative assignment (+ =, -=) and iterators. I want to do it.

Reference book

I have attached the GitHub published from the book I referred to. Supplementary revision Python scraping & machine learning development technique

Recommended Posts

Learning record (4th day) #How to get the absolute path from the relative path
Learning record 4 (8th day)
Learning record 9 (13th day)
Learning record 3 (7th day)
Learning record 5 (9th day)
Learning record 6 (10th day)
Learning record 8 (12th day)
Learning record 1 (4th day)
Learning record 7 (11th day)
Learning record 2 (6th day)
Learning record 16 (20th day)
Learning record 22 (26th day)
[Selenium] How to specify the relative path of chromedriver?
Learning record No. 21 (25th day)
Learning record 13 (17th day) Kaggle3
Learning record No. 10 (14th day)
Learning record 12 (16th day) Kaggle2
Learning record No. 24 (28th day)
Learning record No. 23 (27th day)
Learning record No. 25 (29th day)
Learning record No. 26 (30th day)
Learning record No. 20 (24th day)
How to get the Python version
Learning record No. 14 (18th day) Kaggle4
Learning record No. 15 (19th day) Kaggle5
Learning record 11 (15th day) Kaggle participation
How to get followers and followers from python using the Mastodon API
Record the steps to understand machine learning
Get the path to the systemd unit file
How to get colored output to the console
How to operate Linux from the console
How to access the Datastore from the outside
[Python Kivy] How to get the file path by dragging and dropping
How to get results from id in Celery
How to operate Linux from the outside Procedure
How to measure line speed from the terminal
How to get the files in the [Python] folder
Note: How to get the last day of the month with python (added the first day of the month)
To get the path of the currently running python.exe
[Python] How to remove duplicate values from the list
How to get the variable name itself in python
How to get the number of digits in Python
Get the absolute path of the script you are running
How to instantly launch Jupyter Notebook from the terminal
How to post a ticket from the Shogun API
Programming learning record day 2
Get data from Poloniex, a cryptocurrency exchange, via API and use deep learning to forecast prices for the next day.
[Deep learning] Investigating how to use each function of the convolutional neural network [DW day 3]
How to get only the data you need from a structured data set using a versatile method
[Part 1] Use Deep Learning to forecast the weather from weather images
[Part 3] Use Deep Learning to forecast the weather from weather images
A story about how to specify a relative path in python.
Access the file with a relative path from the execution script.
Get the package version to register with PyPI from Git
How to calculate the amount of calculation learned from ABC134-D
How to log in automatically like 1Password from the CLI
(Note) How to pass the path of your own module
How to do the initial setup from Django project creation
How to increase the number of machine learning dataset images
How to get the last (last) value in a list in Python
How to get all the keys and values in the dictionary