在Python代码中,查找所有字符串可以使用正则表达式库re
。以下是一个示例代码,用于查找Python代码文件中的所有字符串:
import re
def find_strings_in_file(file_path):
with open(file_path, 'r') as file:
content = file.read()
# 使用正则表达式查找所有字符串
strings = re.findall(r'\".*?\"|\'.*?\'', content)
return strings
file_path = 'your_python_file_path.py'
strings = find_strings_in_file(file_path)
print(strings)
将your_python_file_path.py
替换为您要查找的Python代码文件的路径。这个函数将返回一个列表,其中包含了代码文件中的所有字符串。
领取专属 10元无门槛券
手把手带您无忧上云