这是一个可以集成到代码中的解决方案。您会将其应用于您阅读的每一行(或您认为需要像这样解析的每一行),
def get_substring(input_string, delim, nth, delims):
''' Returns the substring between the nth character
delim in the string and next such character;
delims is a list of all delimiters to account for '''
# Indices of all occurences of delims
idx_delims = [i for i, x in enumerate(input_string) if x in delims]
# Retrieve the index of nth delim
idx_nth = idx_delims[nth-1]
# Find the index of the nth+1 delim
idx_nth_p1 = input_string.index(delim, idx_nth+1)
# Return the substring between those two positions
return input_string[idx_nth+1:idx_nth_p1]
orig_string = '5d:6g:9h:5t:7a:45;33:12:5B:9J;70;9C;89;85:4B:38:16:9B:45:56:85:'
print(orig_string)
# All delimiters
delims = [':', ';']
# Substring between 10th and 11th :
str_1 = get_substring(orig_string, ';', 10, delims)
print(str_1)
# Substring between 15th and 16th ;
str_2 = get_substring(orig_string, ':', 15, delims)
print(str_2)
此函数从输入字符串中提取所有被视为分隔符的字符。然后它根据请求找到第 N 个分隔符,以及原始字符串中的下一个分隔符。它返回介于两者之间的字符串。
实际上,这应该有一些检查功能,以及相关的警告,甚至抛出异常(例如,delim 是否存在,以及它是否在请求的nth 位置)。此外,它可以写得更简洁,为了便于阅读和理解,我把它写得更长。最后,您应该删除最终版本中的打印语句。
更新:这是演示集成的最少代码。您可以单独对其进行测试,然后在原始代码中使用读取和后处理方法而不是 open 和 readlines。两者都没有错,但是:
-
open 子句需要 close 和 with open 在幕后为您提供 close,即使事情崩溃了。
-
readlines 读取整个文件。我经常处理大文件,所以我习惯于节省内存并逐行处理。这取决于您,以及您正在解决的问题。
下面是例子:
def get_substring(input_string, delim, nth, delims):
''' Returns the substring between the nth character
delim in the string and next such character;
delims is a list of all delimiters to account for '''
# Indices of all occurences of delims
idx_delims = [i for i, x in enumerate(input_string) if x in delims]
# Retrieve the index of nth delim
idx_nth = idx_delims[nth-1]
# Find the index of the nth+1 delim
idx_nth_p1 = input_string.index(delim, idx_nth+1)
# Return the substring between those two positions
return input_string[idx_nth+1:idx_nth_p1]
# All delimiters
delims = [':', ';']
all_substrings = []
with open('testfile.txt', 'r') as fin:
for line in fin:
# Remove the leading and trailing whitespace
line = line.strip()
temp_str = get_substring(line, ':', 2, delims)
all_substrings.append(temp_str)
print(all_substrings)
代码用strip() 清除尾随换行符,并将所有子字符串附加到列表中。
注意:按照您描述问题的方式,在我看来,您想在一个包含所有分隔符的位置匹配特定分隔符,即对于此 5d:6g:9h:5t:7a:45;33:12:,分隔符 ; 将是第 6 个定界符,所以调用转向(line, ';', 6, delims)。如果不是这种情况,请告诉我,但请考虑自行调整以进行练习。这意味着您在评论中提到的电话应该就像这里一样,(line, ':', 2, delims)。因为: 是第二个分隔符。还要记住,Python 索引从 0 开始,所以这实际上是 idx_delims 列表中的位置 1。
最后,这是一个用于测试的最小输入文件:
5d:6g:9h:5t:7a:45;33:12:5B:9J;70;9C;89;85:4B:38:16:9B:45:56:85:
5d:6g:9h:4t:7a:45;33:12:5B:9J;70;9C;89;85:4B:38:16:9B:45:56:85:
3d:7g:9i:5t:7a:45;33:12:5B:9J;70;9C;89;85:4B:38:16:9B:45:56:85: