英文:
How to find all instances of an unterminated percent sign in a batch file?
问题
这是您要翻译的代码部分:
def check_unterminated_percent_signs(content):
# check for unterminated percent signs
errors = []
for i, line in enumerate(content.split('\n')):
match = re.search(r'%[^%\n]*$', line)
if match:
errors.append(f'Unterminated percent sign found on line {i+1}, position {match.start()}: {line.strip()}')
return errors
以下是已经翻译好的内容:
我有一个Python函数,用于查找批处理文件中的潜在错误,我正在尝试解决的问题之一是查找任何具有未终止百分号的行。然而,此函数似乎返回了每个具有百分号的行上的每个“%”符号。
目前的函数如下,但它没有完成我需要的任务:
以下是不应触发错误的批处理文件中的一行:
set Log_File=%Backup_Folder%\Logs\daily_copy.log
英文:
I have a python function that looks through a batch file for potential errors, and the one that I am looking to resolve is to find any lines that have an unterminated percent sign. However, this function seems to be returning every %
sign on every line that has a %
sign.
Here is the function I have at the moment, which is not doing what I need it to do:
def check_unterminated_percent_signs(content):
# check for unterminated percent signs
errors = []
for i, line in enumerate(content.split('\n')):
match = re.search(r'%[^%\n]*$', line)
if match:
errors.append(f'Unterminated percent sign found on line {i+1}, position {match.start()}: {line.strip()}')
return errors
<br>
Here is a line from the batch file that should not trigger the error:
set Log_File=%Backup_Folder%\Logs\daily_copy.log
答案1
得分: 0
这是解决我的问题的代码:
def check_unterminated_percent_signs(content):
# 检查未终止的百分号符号
errors = []
for i, line in enumerate(content.split('\n')):
num_percent_signs = line.count('%')
if num_percent_signs % 2 == 1:
errors.append(f'第{i+1}行发现未终止的百分号符号: {line.strip()}')
return errors
英文:
Thanks DarkKnight,
This is the code that solved my problem:
def check_unterminated_percent_signs(content):
# check for unterminated percent signs
errors = []
for i, line in enumerate(content.split('\n')):
num_percent_signs = line.count('%')
if num_percent_signs % 2 == 1:
errors.append(f'Unterminated percent sign found on line {i+1}: {line.strip()}')
return errors
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论