| 52 | json.dump(data, f, ensure_ascii=False) |
| 53 | f.write('\n') # 每个字典单独占一行 |
| 54 | def extract_substring(text, str1, str2=""): |
| 55 | # 查找 str1 的最后一次出现的位置 |
| 56 | index1 = text.find(str1) |
| 57 | # 如果 str1 找不到,直接返回空字符串 |
| 58 | if index1 == -1: |
| 59 | return "" |
| 60 | # 如果 str2 为空,返回从 str1 最后一个字符之后到文本末尾的所有内容 |
| 61 | if not str2: |
| 62 | return text[index1 + len(str1):] |
| 63 | # 查找 str2 第一次出现的位置 |
| 64 | index2 = text.find(str2) |
| 65 | # 如果 str2 找不到,返回空字符串 |
| 66 | if index2 == -1: |
| 67 | return "" |
| 68 | # 确保提取的区间合法 |
| 69 | start = index1 + len(str1) # 从 str1 的最后一个字符之后开始 |
| 70 | end = index2 # 到 str2 的开始位置之前 |
| 71 | if start < end: # 如果区间合法,返回截取的内容 |
| 72 | return text[start:end] |
| 73 | else: |
| 74 | return "" # 如果区间无效,返回空字符串 |
| 75 | def extract_substring2(text, start_str, stop_strs): |
| 76 | # 查找 start_str 的最后一次出现的位置 |
| 77 | start_index = text.find(start_str) |