Chapter 12: Regular expressions | Python for Everybody 讲义笔记_En
文章目录
- Python for Everybody
- 课程简介
- Regular Expressions
- Regular Expressions
- Character matching in regular expressions
- Extracting data using regular expressions
- Combining searching and extracting
- Escape character
- Summary
- Bonus section for Unix / Linux users
- Debugging
- Python Regular Expression Quick Guide
- Glossary
- Exercises
Python for Everybody
Exploring Data Using Python 3
Dr. Charles R. Severance
课程简介
Python for Everybody 零基础程序设计(Python 入门)
- This course aims to teach everyone the basics of programming computers using Python. 本课程旨在向所有人传授使用 Python 进行计算机编程的基础知识。
- We cover the basics of how one constructs a program from a series of simple instructions in Python. 我们介绍了如何通过 Python 中的一系列简单指令构建程序的基础知识。
- The course has no pre-requisites and avoids all but the simplest mathematics. Anyone with moderate computer experience should be able to master the materials in this course. 该课程没有任何先决条件,除了最简单的数学之外,避免了所有内容。任何具有中等计算机经验的人都应该能够掌握本课程中的材料。
- This course will cover Chapters 1-5 of the textbook “Python for Everybody”. Once a student completes this course, they will be ready to take more advanced programming courses. 本课程将涵盖《Python for Everyday》教科书的第 1-5 章。学生完成本课程后,他们将准备好学习更高级的编程课程。
- This course covers Python 3.
coursera
Python for Everybody 零基础程序设计(Python 入门)
Charles Russell Severance
Clinical Professor
个人主页
Twitter
University of Michigan
课程资源
coursera原版课程视频
coursera原版视频-中英文精校字幕-B站
Dr. Chuck官方翻录版视频-机器翻译字幕-B站
PY4E-课程配套练习
Dr. Chuck Online - 系列课程开源官网
Regular Expressions
Regular Expressions allow us to search for patterns in strings and extract data from strings using the regular expression programming language.
Regular Expressions
So far we have been reading through files, looking for patterns and extracting various bits of lines that we find interesting. We have been using string methods like split
and find
and using lists and string slicing to extract portions of the lines.
This task of searching and extracting is so common that Python has a very powerful module called regular expressions that handles many of these tasks quite elegantly. The reason we have not introduced regular expressions earlier in the book is because while they are very powerful, they are a little complicated and their syntax takes some getting used to.
Regular expressions are almost their own little programming language for searching and parsing strings. As a matter of fact, entire books have been written on the topic of regular expressions. In this chapter, we will only cover the basics of regular expressions. For more detail on regular expressions, see:
https://en.wikipedia.org/wiki/Regular_expression
https://docs.python.org/library/re.html
The regular expression module re
must be imported into your program before you can use it. The simplest use of the regular expression module is the search()
function. The following program demonstrates a trivial use of the search function.
# Search for lines that contain 'From'
import re
hand = open('mbox-short.txt')
for line in hand:line = line.rstrip()if re.search('From:', line):print(line)# Code: http://www.py4e.com/code3/re01.py
We open the file, loop through each line, and use the regular expression search()
to only print out lines that contain the string “From:”. This program does not use the real power of regular expressions, since we could have just as easily used line.find()
to accomplish the same result.
The power of the regular expressions comes when we add special characters to the search string that allow us to more precisely control which lines match the string. Adding these special characters to our regular expression allow us to do sophisticated matching and extraction while writing very little code.
For example, the caret character is used in regular expressions to match “the beginning” of a line. We could change our program to only match lines where “From:” was at the beginning of the line as follows:
# Search for lines that start with 'From'
import re
hand = open('mbox-short.txt')
for line in hand:line = line.rstrip()if re.search('^From:', line):print(line)# Code: http://www.py4e.com/code3/re02.py
Now we will only match lines that start with the string “From:”. This is still a very simple example that we could have done equivalently with the startswith()
method from the string module. But it serves to introduce the notion that regular expressions contain special action characters that give us more control as to what will match the regular expression.
Character matching in regular expressions
There are a number of other special characters that let us build even more powerful regular expressions. The most commonly used special character is the period or full stop, which matches any character.
In the following example, the regular expression F..m:
would match any of the strings “From:”, “Fxxm:”, “F12m:”, or “F!@m:” since the period characters in the regular expression match any character.
# Search for lines that start with 'F', followed by
# 2 characters, followed by 'm:'
import re
hand = open('mbox-short.txt')
for line in hand:line = line.rstrip()if re.search('^F..m:', line):print(line)# Code: http://www.py4e.com/code3/re03.py
This is particularly powerful when combined with the ability to indicate that a character can be repeated any number of times using the *
or +
characters in your regular expression. These special characters mean that instead of matching a single character in the search string, they match zero-or-more characters (in the case of the asterisk) or one-or-more of the characters (in the case of the plus sign).
We can further narrow down the lines that we match using a repeated wild card character in the following example:
# Search for lines that start with From and have an at sign
import re
hand = open('mbox-short.txt')
for line in hand:line = line.rstrip()if re.search('^From:.+@', line):print(line)# Code: http://www.py4e.com/code3/re04.py
The search string ^From:.+@
will successfully match lines that start with “From:”, followed by one or more characters (.+
), followed by an at-sign. So this will match the following line:
From: stephen.marquard@uct.ac.za
You can think of the .+
wildcard as expanding to match all the characters between the colon character and the at-sign.
From:.+@
It is good to think of the plus and asterisk characters as “pushy”. For example, the following string would match the last at-sign in the string as the .+
pushes outwards, as shown below:
From: stephen.marquard@uct.ac.za, csev@umich.edu, and cwen @iupui.edu
It is possible to tell an asterisk or plus sign not to be so “greedy” by adding another character. See the detailed documentation for information on turning off the greedy behavior.
Extracting data using regular expressions
If we want to extract data from a string in Python we can use the findall()
method to extract all of the substrings which match a regular expression. Let’s use the example of wanting to extract anything that looks like an email address from any line regardless of format. For example, we want to pull the email addresses from each of the following lines:
From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008
Return-Path: <postmaster@collab.sakaiproject.org>for <source@collab.sakaiproject.org>;
Received: (from apache@localhost)
Author: stephen.marquard@uct.ac.za
We don’t want to write code for each of the types of lines, splitting and slicing differently for each line. This following program uses findall()
to find the lines with email addresses in them and extract one or more addresses from each of those lines.
import re
s = 'A message from csev@umich.edu to cwen@iupui.edu about meeting @2PM'
lst = re.findall('\S+@\S+', s)
print(lst)# Code: http://www.py4e.com/code3/re05.py
The findall()
method searches the string in the second argument and returns a list of all of the strings that look like email addresses. We are using a two-character sequence that matches a non-whitespace character (\S
).
The output of the program would be:
['csev@umich.edu', 'cwen@iupui.edu']
Translating the regular expression, we are looking for substrings that have at least one non-whitespace character, followed by an at-sign, followed by at least one more non-whitespace character. The \S+
matches as many non-whitespace characters as possible.
The regular expression would match twice (csev@umich.edu and cwen@iupui.edu), but it would not match the string “@2PM” because there are no non-blank characters before the at-sign. We can use this regular expression in a program to read all the lines in a file and print out anything that looks like an email address as follows:
# Search for lines that have an at sign between characters
import re
hand = open('mbox-short.txt')
for line in hand:line = line.rstrip()x = re.findall('\S+@\S+', line)if len(x) > 0:print(x)# Code: http://www.py4e.com/code3/re06.py
We read each line and then extract all the substrings that match our regular expression. Since findall()
returns a list, we simply check if the number of elements in our returned list is more than zero to print only lines where we found at least one substring that looks like an email address.
If we run the program on mbox-short.txt we get the following output:
...
['<source@collab.sakaiproject.org>;']
['<source@collab.sakaiproject.org>;']
['apache@localhost)']
['source@collab.sakaiproject.org;']
['cwen@iupui.edu']
['source@collab.sakaiproject.org']
['cwen@iupui.edu']
['cwen@iupui.edu']
['wagnermr@iupui.edu']
Some of our email addresses have incorrect characters like “<” or “;” at the beginning or end. Let’s declare that we are only interested in the portion of the string that starts and ends with a letter or a number.
To do this, we use another feature of regular expressions. Square brackets are used to indicate a set of multiple acceptable characters we are willing to consider matching. In a sense, the \S
is asking to match the set of “non-whitespace characters”. Now we will be a little more explicit in terms of the characters we will match.
Here is our new regular expression:
[a-zA-Z0-9]\S*@\S*[a-zA-Z]
This is getting a little complicated and you can begin to see why regular expressions are their own little language unto themselves. Translating this regular expression, we are looking for substrings that start with a single lowercase letter, uppercase letter, or number “[a-zA-Z0-9]”, followed by zero or more non-blank characters (\S*
), followed by an at-sign, followed by zero or more non-blank characters (\S*
), followed by an uppercase or lowercase letter. Note that we switched from +
to *
to indicate zero or more non-blank characters since [a-zA-Z0-9]
is already one non-blank character. Remember that the *
or +
applies to the single character immediately to the left of the plus or asterisk.
If we use this expression in our program, our data is much cleaner:
# Search for lines that have an at sign between characters
# The characters must be a letter or number
import re
hand = open('mbox-short.txt')
for line in hand:line = line.rstrip()x = re.findall('[a-zA-Z0-9]\S*@\S*[a-zA-Z]', line)if len(x) > 0:print(x)# Code: http://www.py4e.com/code3/re07.py
...
['wagnermr@iupui.edu']
['cwen@iupui.edu']
['postmaster@collab.sakaiproject.org']
['200801032122.m03LMFo4005148@nakamura.uits.iupui.edu']
['source@collab.sakaiproject.org']
['source@collab.sakaiproject.org']
['source@collab.sakaiproject.org']
['apache@localhost']
Notice that on the source@collab.sakaiproject.org
lines, our regular expression eliminated two letters at the end of the string (“>;”). This is because when we append [a-zA-Z]
to the end of our regular expression, we are demanding that whatever string the regular expression parser finds must end with a letter. So when it sees the “>” at the end of “sakaiproject.org>;” it simply stops at the last “matching” letter it found (i.e., the “g” was the last good match).
Also note that the output of the program is a Python list that has a string as the single element in the list.
Combining searching and extracting
If we want to find numbers on lines that start with the string “X-” such as:
X-DSPAM-Confidence: 0.8475
X-DSPAM-Probability: 0.0000
we don’t just want any floating-point numbers from any lines. We only want to extract numbers from lines that have the above syntax.
We can construct the following regular expression to select the lines:
^X-.*: [0-9.]+
Translating this, we are saying, we want lines that start with X-
, followed by zero or more characters (.*
), followed by a colon (:
) and then a space. After the space we are looking for one or more characters that are either a digit (0-9) or a period [0-9.]+
. Note that inside the square brackets, the period matches an actual period (i.e., it is not a wildcard between the square brackets).
This is a very tight expression that will pretty much match only the lines we are interested in as follows:
# Search for lines that start with 'X' followed by any non
# whitespace characters and ':'
# followed by a space and any number.
# The number can include a decimal.
import re
hand = open('mbox-short.txt')
for line in hand:line = line.rstrip()if re.search('^X\S*: [0-9.]+', line):print(line)# Code: http://www.py4e.com/code3/re10.py
When we run the program, we see the data nicely filtered to show only the lines we are looking for.
X-DSPAM-Confidence: 0.8475
X-DSPAM-Probability: 0.0000
X-DSPAM-Confidence: 0.6178
X-DSPAM-Probability: 0.0000
...
But now we have to solve the problem of extracting the numbers. While it would be simple enough to use split
, we can use another feature of regular expressions to both search and parse the line at the same time.
Parentheses are another special character in regular expressions. When you add parentheses to a regular expression, they are ignored when matching the string. But when you are using findall()
, parentheses indicate that while you want the whole expression to match, you only are interested in extracting a portion of the substring that matches the regular expression.
So we make the following change to our program:
# Search for lines that start with 'X' followed by any
# non whitespace characters and ':' followed by a space
# and any number. The number can include a decimal.
# Then print the number if it is greater than zero.
import re
hand = open('mbox-short.txt')
for line in hand:line = line.rstrip()x = re.findall('^X\S*: ([0-9.]+)', line)if len(x) > 0:print(x)# Code: http://www.py4e.com/code3/re11.py
Instead of calling search()
, we add parentheses around the part of the regular expression that represents the floating-point number to indicate we only want findall()
to give us back the floating-point number portion of the matching string.
The output from this program is as follows:
['0.8475']
['0.0000']
['0.6178']
['0.0000']
['0.6961']
['0.0000']
...
The numbers are still in a list and need to be converted from strings to floating point, but we have used the power of regular expressions to both search and extract the information we found interesting.
As another example of this technique, if you look at the file there are a number of lines of the form:
Details: http://source.sakaiproject.org/viewsvn/?view=rev&rev=39772
If we wanted to extract all of the revision numbers (the integer number at the end of these lines) using the same technique as above, we could write the following program:
# Search for lines that start with 'Details: rev='
# followed by numbers
# Then print the number if one is found
import re
hand = open('mbox-short.txt')
for line in hand:line = line.rstrip()x = re.findall('^Details:.*rev=([0-9]+)', line)if len(x) > 0:print(x)# Code: http://www.py4e.com/code3/re12.py
Translating our regular expression, we are looking for lines that start with Details:
, followed by any number of characters (.*
), followed by rev=
, and then by one or more digits. We want to find lines that match the entire expression but we only want to extract the integer number at the end of the line, so we surround [0-9]
+ with parentheses.
When we run the program, we get the following output:
['39772']
['39771']
['39770']
['39769']
...
Remember that the [0-9]+
is “greedy” and it tries to make as large a string of digits as possible before extracting those digits. This “greedy” behavior is why we get all five digits for each number. The regular expression module expands in both directions until it encounters a non-digit, or the beginning or the end of a line.
Now we can use regular expressions to redo an exercise from earlier in the book where we were interested in the time of day of each mail message. We looked for lines of the form:
From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008
and wanted to extract the hour of the day for each line. Previously we did this with two calls to split
. First the line was split into words and then we pulled out the fifth word and split it again on the colon character to pull out the two characters we were interested in.
While this worked, it actually results in pretty brittle code that is assuming the lines are nicely formatted. If you were to add enough error checking (or a big try/except block) to insure that your program never failed when presented with incorrectly formatted lines, the code would balloon to 10-15 lines of code that was pretty hard to read.
We can do this in a far simpler way with the following regular expression:
^From .* [0-9][0-9]:
The translation of this regular expression is that we are looking for lines that start with From
(note the space), followed by any number of characters (.*
), followed by a space, followed by two digits [0-9][0-9]
, followed by a colon character. This is the definition of the kinds of lines we are looking for.
In order to pull out only the hour using findall()
, we add parentheses around the two digits as follows:
^From .* ([0-9][0-9]):
This results in the following program:
# Search for lines that start with From and a character
# followed by a two digit number between 00 and 99 followed by ':'
# Then print the number if one is found
import re
hand = open('mbox-short.txt')
for line in hand:line = line.rstrip()x = re.findall('^From .* ([0-9][0-9]):', line)if len(x) > 0: print(x)# Code: http://www.py4e.com/code3/re13.py
When the program runs, it produces the following output:
['09']
['18']
['16']
['15']
...
Escape character
Since we use special characters in regular expressions to match the beginning or end of a line or specify wild cards, we need a way to indicate that these characters are “normal” and we want to match the actual character such as a dollar sign or caret.
We can indicate that we want to simply match a character by prefixing that character with a backslash. For example, we can find money amounts with the following regular expression.
import re
x = 'We just received $10.00 for cookies.'
y = re.findall('\$[0-9.]+',x)
Since we prefix the dollar sign with a backslash, it actually matches the dollar sign in the input string instead of matching the “end of line”, and the rest of the regular expression matches one or more digits or the period character. Note: Inside square brackets, characters are not “special”. So when we say [0-9.]
, it really means digits or a period. Outside of square brackets, a period is the “wild-card” character and matches any character. Inside square brackets, the period is a period.
Summary
While this only scratched the surface of regular expressions, we have learned a bit about the language of regular expressions. They are search strings with special characters in them that communicate your wishes to the regular expression system as to what defines “matching” and what is extracted from the matched strings. Here are some of those special characters and character sequences:
^
Matches the beginning of the line.
$
Matches the end of the line.
.
Matches any character (a wildcard).
\s
Matches a whitespace character.
\S
Matches a non-whitespace character (opposite of \s).
*
Applies to the immediately preceding character(s) and indicates to match zero or more times.
*?
Applies to the immediately preceding character(s) and indicates to match zero or more times in “non-greedy mode”.
+
Applies to the immediately preceding character(s) and indicates to match one or more times.
+?
Applies to the immediately preceding character(s) and indicates to match one or more times in “non-greedy mode”.
?
Applies to the immediately preceding character(s) and indicates to match zero or one time.
??
Applies to the immediately preceding character(s) and indicates to match zero or one time in “non-greedy mode”.
[aeiou]
Matches a single character as long as that character is in the specified set. In this example, it would match “a”, “e”, “i”, “o”, or “u”, but no other characters.
[a-z0-9]
You can specify ranges of characters using the minus sign. This example is a single character that must be a lowercase letter or a digit.
[^A-Za-z]
When the first character in the set notation is a caret, it inverts the logic. This example matches a single character that is anything other than an uppercase or lowercase letter.
( )
When parentheses are added to a regular expression, they are ignored for the purpose of matching, but allow you to extract a particular subset of the matched string rather than the whole string when using findall()
.
\b
Matches the empty string, but only at the start or end of a word.
\B
Matches the empty string, but not at the start or end of a word.
\d
Matches any decimal digit; equivalent to the set [0-9].
\D
Matches any non-digit character; equivalent to the set [^0-9].
Bonus section for Unix / Linux users
Support for searching files using regular expressions was built into the Unix operating system since the 1960s and it is available in nearly all programming languages in one form or another.
As a matter of fact, there is a command-line program built into Unix called grep (Generalized Regular Expression Parser) that does pretty much the same as the search()
examples in this chapter. So if you have a Macintosh or Linux system, you can try the following commands in your command-line window.
$ grep '^From:' mbox-short.txt
From: stephen.marquard@uct.ac.za
From: louis@media.berkeley.edu
From: zqian@umich.edu
From: rjlowe@iupui.edu
This tells grep
to show you lines that start with the string “From:” in the file mbox-short.txt. If you experiment with the grep
command a bit and read the documentation for grep
, you will find some subtle differences between the regular expression support in Python and the regular expression support in grep
. As an example, grep
does not support the non-blank character \S
so you will need to use the slightly more complex set notation [^ ]
, which simply means match a character that is anything other than a space.
Debugging
Python has some simple and rudimentary built-in documentation that can be quite helpful if you need a quick refresher to trigger your memory about the exact name of a particular method. This documentation can be viewed in the Python interpreter in interactive mode.
You can bring up an interactive help system using help()
.
>>> help()help> modules
If you know what module you want to use, you can use the dir()
command to find the methods in the module as follows:
>>> import re
>>> dir(re)
[.. 'compile', 'copy_reg', 'error', 'escape', 'findall',
'finditer', 'match', 'purge', 'search', 'split', 'sre_compile',
'sre_parse', 'sub', 'subn', 'sys', 'template']
You can also get a small amount of documentation on a particular method using the help command combined with the desired method.
>>> help (re.search)
Help on function search in module re:search(pattern, string, flags=0)Scan through string looking for a match to the pattern, returninga match object, or None if no match was found.
>>>
The built-in documentation is not very extensive, but it can be helpful when you are in a hurry or don’t have access to a web browser or search engine.
Python Regular Expression Quick Guide
Python Regular Expression Quick Guide
Python Regular Expression Quick Guide^ Matches the beginning of a line
$ Matches the end of the line
. Matches any character
\s Matches whitespace
\S Matches any non-whitespace character
* Repeats a character zero or more times
*? Repeats a character zero or more times (non-greedy)
+ Repeats a character one or more times
+? Repeats a character one or more times (non-greedy)
[aeiou] Matches a single character in the listed set
[^XYZ] Matches a single character not in the listed set
[a-z0-9] The set of characters can include a range
( Indicates where string extraction is to start
) Indicates where string extraction is to end
Glossary
brittle code
Code that works when the input data is in a particular format but is prone to breakage if there is some deviation from the correct format. We call this “brittle code” because it is easily broken.
greedy matching
The notion that the + and * characters in a regular expression expand outward to match the largest possible string.
grep
A command available in most Unix systems that searches through text files looking for lines that match regular expressions. The command name stands for “Generalized Regular Expression Parser”.
regular expression
A language for expressing more complex search strings. A regular expression may contain special characters that indicate that a search only matches at the beginning or end of a line or many other similar capabilities.
wild card
A special character that matches any character. In regular expressions the wild-card character is the period.
Exercises
Exercise 1:
Write a simple program to simulate the operation of the grep command on Unix. Ask the user to enter a regular expression and count the number of lines that matched the regular expression:
$ python grep.py
Enter a regular expression: ^Author
mbox.txt had 1798 lines that matched ^Author$ python grep.py
Enter a regular expression: ^X-
mbox.txt had 14368 lines that matched ^X-$ python grep.py
Enter a regular expression: java$
mbox.txt had 4175 lines that matched java$
Exercise 2:
Write a program to look for lines of the form:
New Revision: 39772
Extract the number from each of the lines using a regular expression and the findall()
method. Compute the average of the numbers and print out the average as an integer.
Enter file:mbox.txt
38549Enter file:mbox-short.txt
39756
相关文章:

Chapter 12: Regular expressions | Python for Everybody 讲义笔记_En
文章目录 Python for Everybody课程简介Regular ExpressionsRegular ExpressionsCharacter matching in regular expressionsExtracting data using regular expressionsCombining searching and extractingEscape characterSummaryBonus section for Unix / Linux usersDebugg…...
Android javaMail mergeDebugJavaResource FAILED解决
Java mail 引入这两个jar之后 implementation com.sun.mail:android-mail:1.6.7implementation com.sun.mail:android-activation:1.6.7build直接报错 > Task :app:mergeDebugJavaResource FAILED Execution failed for task :app:mergeDebugJavaResource. > A failure o…...

【ArcGIS Pro二次开发】(57):地图系列
在ArcGIS Pro中,有一个地图系列,可以在一个布局中导出多个地图。 在SDK中为ArcGIS.Desktop.layout.MapSeries类和映射系列导出选项,可以以支持多页导出。 MapSeries类提供了一个静态CreateSpatialMapSeries方法,该方法使用指定的…...
秋招打卡015(20230811)
文章目录 前言一、今天学习了什么?二、动态规划之股票问题1、总结2、题目 三、SQL总结 前言 提示:这里为每天自己的学习内容心情总结; Learn By Doing,Now or Never,Writing is organized thinking. 提示:…...

如何使用Word转PDF转换器在线工具?在线Word转PDF使用方法
Word转PDF转换器在线,是一种方便快捷的工具,可帮助您在不需要下载任何软件的情况下完成此任务。无论您是需要在工作中共享文档,还是将文件以PDF格式保存以确保格式不变,都可以依靠这款在线工具轻松完成转换。那么如何使用Word转PD…...
自然语言处理从入门到应用——LangChain:记忆(Memory)-[记忆的类型Ⅰ]
分类目录:《自然语言处理从入门到应用》总目录 会话缓存记忆ConversationBufferMemory 本节将介绍如何使用对话缓存记忆ConversationBufferMemory。这种记忆方式允许存储消息,并将消息提取到一个变量中,我们首先将其提取为字符串:…...
Camunda 7.x 系列【7】Spring Boot 集成 Camunda 7.19
有道无术,术尚可求,有术无道,止于术。 本系列Spring Boot 版本 2.7.9 本系列Camunda 版本 7.19.0 源码地址:https://gitee.com/pearl-organization/camunda-study-demo 文章目录 1. 前言2. Camunda Platform Run3. Spring Boot 版本兼容性4. 集成 Spring Boot5. 启动项目…...
24华东交通软件工程837考研题库
1.Jackson设计方法是由英国的M.Jackson所提出的。它是一种面向( )的软件设 计方法。 A.对象 B.数据流 C.数据结构 D.控制结构 答案:C 2.软件设计中,Jackson方法是一种面向…...

nginx 以及nginx优化
目录 nginx功能介绍 静态文件服务 反向代理 动态内容处理 SSL/TLS 加密支持 虚拟主机支持 URL 重写和重定向 缓存机制 日志记录 可扩展性和灵活性 nginx的主要应用场景 nginx常用命令 nginx另外一种安装方式 nginx常用的信号符: nginx配置文件详解 n…...

cesium学习记录04-坐标系
一、地理坐标系和投影坐标系的关系 地理坐标系 (Geographic Coordinate System, GCS) 定义:地理坐标系是一个基于三维地球表面的坐标系统。它使用经度和纬度来表示地点的位置。 特点: 使用经纬度来定义位置。 基于特定的地球参考椭球体。 适用于全球范…...
P5737 【深基7.例3】闰年展示
题目描述 输入 x , y x,y x,y,输出 [ x , y ] [x,y] [x,y] 区间中闰年个数,并在下一行输出所有闰年年份数字,使用空格隔开。 输入格式 输入两个正整数 x , y x,y x,y,以空格隔开。 输出格式 第一行输出一个正整数…...
Nacos的安装使用教程Linux
在 Linux 操作系统上安装和使用 Nacos 与 Windows 类似,以下是详细的步骤教程。我们将使用 Nacos 的 Standalone 模式进行安装和使用。请注意,对于生产环境,建议使用集群模式来实现高可用性和可扩展性。 步骤 1:准备环境 Java 安…...
数据结构-学习
参考: 数据结构-学习笔记_蓝净云_蓝净云的博客-CSDN博客...

【MFC】05.MFC六大机制:程序启动机制-笔记
MFC程序开发所谓是非常简单,但是对于我们逆向人员来说,如果想要逆向MFC程序,那么我们就必须了解它背后的机制,这样我们才能够清晰地逆向出MFC程序,今天这篇文章就来带领大家了解MFC的第一大机制:程序启动机…...

Von Maur, Inc EDI 需求分析
Von Maur, Inc 是一家历史悠久的卖场,成立于19世纪,总部位于美国。作为一家知名的零售商,Von Maur 主要经营高端时装、家居用品和美妆产品。其使命是为顾客提供优质的产品和无与伦比的购物体验。多年来,Von Maur 凭借其卓越的服务…...

[深度学习入门]PyTorch深度学习[Numpy基础](上)
目录 一、前言二、Numpy概述三、生成Numpy数组3.1 从已有数据中创建数组3.2 利用random模块生成数组3.3 创建特定形状的多维数组3.4 利用arange和linspace函数生成数组 四、获取元素五、Numpy的算术运算5.1 对应元素相乘5.2 点积运算 六、后记 本文的目标受众: 对机…...
Excel vost 实现照光灯效果
如果你想要在 VSTO(Visual Studio Tools for Office)中实现在 Excel 中添加“照光灯”效果,你需要创建一个 VSTO 插件来实现这个功能。照光灯效果通常是指通过将非活动行或列进行高亮显示,以便更清楚地查看某一行或列的内容。以下…...

IntelliJ中文乱码问题
1、控制台乱码 运行时控制台输出的中文为乱码,解决方法:帮助 > 编辑自定义虚拟机选项… > 此时会自动创建出一个新文件,输入:-Dfile.encodingUTF-8,然后重启IDE即可,操作截图如下: 2、…...

【C++】红黑树模拟实现插入功能(包含旋转和变色)
红黑树模拟实现并封装为map和set 前言正式开始红黑树概念红黑树基本要求大致框架树节点树 调整红黑树使其平衡第一种:cur红,p红,g黑,u存在且为红第二种:cur红,p红,g黑,u不存在或为黑…...

Pads输出器件坐标文件时,如何更改器件坐标精度
相信对于用pads软件的工程师么,在完成PCB设计的时候都需要输出生产文件给板厂和贴片厂,今天我们需要给大家介绍的是如何在在pads软件上面输出器件坐标文件以及如何更改器件坐标文件的精度。 首先我们需要点击工具-基本脚本-基本脚本接下来会跳到下面这个…...
Django RBAC项目后端实战 - 03 DRF权限控制实现
项目背景 在上一篇文章中,我们完成了JWT认证系统的集成。本篇文章将实现基于Redis的RBAC权限控制系统,为系统提供细粒度的权限控制。 开发目标 实现基于Redis的权限缓存机制开发DRF权限控制类实现权限管理API配置权限白名单 前置配置 在开始开发权限…...
精益数据分析(98/126):电商转化率优化与网站性能的底层逻辑
精益数据分析(98/126):电商转化率优化与网站性能的底层逻辑 在电子商务领域,转化率与网站性能是决定商业成败的核心指标。今天,我们将深入解析不同类型电商平台的转化率基准,探讨页面加载速度对用户行为的…...

Java中HashMap底层原理深度解析:从数据结构到红黑树优化
一、HashMap概述与核心特性 HashMap作为Java集合框架中最常用的数据结构之一,是基于哈希表的Map接口非同步实现。它允许使用null键和null值(但只能有一个null键),并且不保证映射顺序的恒久不变。与Hashtable相比,Hash…...
python读取SQLite表个并生成pdf文件
代码用于创建含50列的SQLite数据库并插入500行随机浮点数据,随后读取数据,通过ReportLab生成横向PDF表格,包含格式化(两位小数)及表头、网格线等美观样式。 # 导入所需库 import sqlite3 # 用于操作…...

若依项目部署--传统架构--未完待续
若依项目介绍 项目源码获取 #Git工具下载 dnf -y install git #若依项目获取 git clone https://gitee.com/y_project/RuoYi-Vue.git项目背景 随着企业信息化需求的增加,传统开发模式存在效率低,重复劳动多等问题。若依项目通过整合主流技术框架&…...
C/Python/Go示例 | Socket Programing与RPC
Socket Programming介绍 Computer networking这个领域围绕着两台电脑或者同一台电脑内的不同进程之间的数据传输和信息交流,会涉及到许多有意思的话题,诸如怎么确保对方能收到信息,怎么应对数据丢失、被污染或者顺序混乱,怎么提高…...
后端下载限速(redis记录实时并发,bucket4j动态限速)
✅ 使用 Redis 记录 所有用户的实时并发下载数✅ 使用 Bucket4j 实现 全局下载速率限制(动态)✅ 支持 动态调整限速策略✅ 下载接口安全、稳定、可监控 🧩 整体架构概览 模块功能Redis存储全局并发数和带宽令牌桶状态Bucket4j Redis分布式限…...
记一次spark在docker本地启动报错
1,背景 在docker中部署spark服务和调用spark服务的微服务,微服务之间通过fegin调用 2,问题,docker容器中服务器来后,注册中心都有,调用服务也正常,但是调用spark启动任务后报错,报错…...
【中间件】Web服务、消息队列、缓存与微服务治理:Nginx、Kafka、Redis、Nacos 详解
Nginx 是什么:高性能的HTTP和反向代理Web服务器。怎么用:通过配置文件定义代理规则、负载均衡、静态资源服务等。为什么用:提升Web服务性能、高并发处理、负载均衡和反向代理。优缺点:轻量高效,但动态处理能力较弱&am…...
Caliper 配置文件解析:config.yaml 和 fisco-bcos.json 附加在caliper中执行不同的合约方法
Caliper 配置文件解析:config.yaml 和 fisco-bcos.json Caliper 是一个区块链性能基准测试工具,用于评估不同区块链平台的性能。下面我将详细解释你提供的 fisco-bcos.json 文件结构,并说明它与 config.yaml 文件的关系。 fisco-bcos.json 文件解析 这个文件是针对 FISCO…...