python 自带 re bug 推荐使用 regex

简介: python 自带 re bug 推荐使用 regex

报错 re.error: bad escape \L at

原因 python 自带的 re 有问题

修改成 regex 即可


安装 regex

pip install regex


使用

import regex as re


关键替换代码 使用 re.sub

str_des_file_0 = re.sub('(?s)<name>.*</name>', r"<name>w</name>", str_file)
images_dir_path = "D:/voc2007/JPEGImages/"
image_path = os.path.join(images_dir_path, i)
path_str = r"<path>{}</path>".format(image_path)
# print(path_str)
str_des_file_1 = re.sub('(?s)<path>.*</path>', path_str, str(str_des_file_0))

替换完整代码

import os, shutil
import regex as re
def mv_floder(path):
  dir_abs_path = os.path.abspath(path)
  res_dir = os.path.dirname(dir_abs_path)
  res_dir = os.path.join(res_dir, "voc2007")
  if os.path.exists(res_dir):
    shutil.rmtree(res_dir)
  os.mkdir(res_dir)
  annotations_dir = os.path.join(res_dir, "Annotations")
  if not os.path.exists(annotations_dir):
    os.mkdir(annotations_dir)
  JPEGImages_dir = os.path.join(res_dir, "JPEGImages")
  if not os.path.exists(JPEGImages_dir):
    os.mkdir(JPEGImages_dir)
  print(dir_abs_path)
  for root, y, files in os.walk(dir_abs_path):
    for i in files:
      file_path = os.path.join(root, i)
      if root.find("annotations") != -1 or root.find("Annotations") != -1:
        des_file_path = os.path.join(res_dir, "Annotations")
        des_file_path = os.path.join(des_file_path, i)
        with open(file_path, "r", encoding="utf8") as f:
          str_file = f.read()
          # print(str_file)
          str_des_file_0 = re.sub('(?s)<name>.*</name>', r"<name>w</name>", str_file)
          images_dir_path = "D:/voc2007/JPEGImages/"
          image_path = os.path.join(images_dir_path, i)
          path_str = r"<path>{}</path>".format(image_path)
          # print(path_str)
          str_des_file_1 = re.sub('(?s)<path>.*</path>', path_str, str(str_des_file_0))
          with open(des_file_path, "w", encoding="utf8") as f_des:
            # print(str_des_file)
            f_des.write(str_des_file_1)
        # shutil.copyfile(file_path, des_file_path)
      if root.find("JPEGImages") != -1:
        des_file_path = os.path.join(res_dir, "JPEGImages")
        des_file_path = os.path.join(des_file_path, i)
        shutil.copyfile(file_path, des_file_path)
if __name__ == "__main__":
  mv_floder("./20200903-1jieguo")
  mv_floder("./20200903-2")



相关文章
Python 内置正则表达式库re的使用
正则表达式是记录文本规则的代码,用于查找和处理符合特定规则的字符串。在Python中,常通过原生字符串`r&#39;string&#39;`表示。使用`re.compile()`创建正则对象,便于多次使用。匹配字符串有`match()`(从开头匹配)、`search()`(搜索首个匹配)和`findall()`(找所有匹配)。替换字符串用`sub()`,分割字符串则用`split()`。
|
9月前
|
Python Windows
【Python进阶必备】一文掌握re库:实战正则表达式
【Python进阶必备】一文掌握re库:实战正则表达式
194 0
|
3月前
|
Python
在Python中,可以使用内置的`re`模块来处理正则表达式
在Python中,可以使用内置的`re`模块来处理正则表达式
89 5
|
4月前
|
Python
Python 中常用的内置模块之`re`模块
【10月更文挑战第11天】 `re` 模块是 Python 内置的正则表达式处理工具,支持模式匹配、搜索、替换等功能。通过 `search`、`match`、`findall` 和 `sub` 等函数,结合正则表达式的元字符、分组、贪婪模式等特性,可高效完成文本处理任务。示例代码展示了基本用法,帮助快速上手。
63 1
|
3月前
|
测试技术 API 数据安全/隐私保护
Python连接到Jira实例、登录、查询、修改和创建bug
通过使用Python和Jira的REST API,可以方便地连接到Jira实例并进行各种操作,包括查询、修改和创建Bug。`jira`库提供了简洁的接口,使得这些操作变得简单易行。无论是自动化测试还是开发工作流的集成,这些方法都可以极大地提高效率和准确性。希望通过本文的介绍,您能够更好地理解和应用这些技术。
344 0
|
5月前
|
Python
Python中正则表达式(re模块)用法详解
Python中正则表达式(re模块)用法详解
89 2
|
6月前
|
Python
|
6月前
|
Python
告别死记硬背:掌握Python正则表达式re模块的高效应用&[面向百度编程]
Python中正则表达式的高效应用,通过内置的`re`模块,讲解了如何匹配、提取和替换字符串,并提供了相关示例代码,同时提倡通过实践来掌握正则表达式的使用,而不是仅仅依赖网络搜索。
69 1
|
6月前
|
SQL 分布式计算 算法
【python】python指南(三):使用正则表达式re提取文本中的http链接
【python】python指南(三):使用正则表达式re提取文本中的http链接
62 0
|
7月前
|
开发者 Python
【Python】已解决:FutureWarning: The default value of regex will change from True to False in a future ver
【Python】已解决:FutureWarning: The default value of regex will change from True to False in a future ver
190 1

热门文章

最新文章