我正在逐行阅读一个文件,假设除了“如果”文件的这一行包含一个大写字母,我什么都不关心。因为如果是的话,我想用它。我不想使用文件的此行,如果它没有。在
我可以用if语句吗?或者我必须使用for循环。我已经有两个嵌套语句了。在
我的代码:
with open(var) as config_file: #open file
for line in config_file: #line by line reading of file
#if "description" and capital letter is contain in line:
line = line.replace('\n' , '').replace('"' , '').replace(']' , '') #removes all extra characters
i = "| except " + (line.split("description ", 1)[1]) + " " #split the line and save last index (cust name)
cus_str+=i #append to string
config_file.close()
data
将只包含大写字符的字符串。在是的。内置的^{} 函数使这一点变得简单:
正则表达式对此可能有点过头了,但它非常简单:
相关问题 更多 >
编程相关推荐