文本文件为空:import docx2txt import re # 读取docx文件内容 text = docx2txt.process("example.docx") # 使用正则表达式匹配文本中以数字和符号.开头到答案加大写英文字母结束的内容保存为列表 pattern = r'\d+\.\s\w+\s\w+\s\w+\s\w+\s\w+\s\w+' result = {} for item in re.findall(pattern, text): result[item] = None # 将去重后的内容写入新的文本文件 with open("example_output.txt", "w", encoding="utf-8") as f: f.write('\n'.join(result.keys()))
时间: 2023-10-03 14:09:17 浏览: 41
可能是因为文本中没有符合正则表达式的内容,导致result字典为空。你可以添加一些代码来判断result字典是否为空,如果为空则输出相应的提示信息。代码如下:
```
import docx2txt
import re
# 读取docx文件内容
text = docx2txt.process("example.docx")
# 使用正则表达式匹配文本中以数字和符号.开头到答案加大写英文字母结束的内容保存为列表
pattern = r'\d+\.\s\w+\s\w+\s\w+\s\w+\s\w+\s\w+'
result = {}
for item in re.findall(pattern, text):
result[item] = None
# 判断result字典是否为空
if not result:
print("没有匹配到符合条件的内容!")
else:
# 将去重后的内容写入新的文本文件
with open("example_output.txt", "w", encoding="utf-8") as f:
f.write('\n'.join(result.keys()))
```
这样当result字典为空时,程序会输出"没有匹配到符合条件的内容!",提醒用户文件中没有符合条件的内容。
相关问题
import docx2txt import re # 读取docx文件内容 text = docx2txt.process("example.docx") # 将内容写入文本文件 with open("example.txt", "w", encoding="utf-8") as f: f.write(text) # 读取文本文件内容 with open("example.txt", "r", encoding="utf-8") as f: lines = f.readlines() # 使用 set 去重 pattern = r'\d.*答案' result = re.findall(pattern, lines) result = list(set(result)) print(result) # 将去重后的内容重新写入文本文件 with open("example.txt", "w", encoding="utf-8") as f: f.writelines(lines)】
这段代码的目的是将一个 `docx` 文件转换为文本文件,并去除其中以数字开头,后面跟着一个句点和6个单词的短语重复项。
首先,使用 `docx2txt` 库将 `docx` 文件转换为文本文件,并使用 `readlines()` 方法将文件中的所有行读入到 `lines` 列表中。
然后,使用正则表达式匹配以数字开头,后面跟着一个句点和6个单词的短语,并使用 `findall()` 方法查找所有匹配的短语。然后,将匹配的结果转换为列表,并使用 `set` 去重。最后,将去重后的结果打印出来。
但是,代码中有一个问题:在使用 `findall()` 方法查找匹配的短语时,应该将正则表达式和文本字符串作为参数传递,而不是将正则表达式和 `lines` 列表作为参数传递。因此,您需要将以下行:
```python
result = re.findall(pattern, lines)
```
修改为:
```python
result = re.findall(pattern, text)
```
同时,为了将去重后的结果写入文本文件,您需要将以下行:
```python
f.writelines(lines)
```
修改为:
```python
f.writelines(result)
```
修改后的代码如下:
```python
import docx2txt
import re
# 读取docx文件内容
text = docx2txt.process("example.docx")
# 将内容写入文本文件
with open("example.txt", "w", encoding="utf-8") as f:
f.write(text)
# 读取文本文件内容
with open("example.txt", "r", encoding="utf-8") as f:
lines = f.readlines()
# 使用正则表达式匹配以数字开头,后面跟着一个句点和6个单词的短语,并去重
pattern = r'\d+\.\s\w+\s\w+\s\w+\s\w+\s\w+\s\w+'
result = re.findall(pattern, text)
result = list(set(result))
print(result)
# 将去重后的内容重新写入文本文件
with open("example.txt", "w", encoding="utf-8") as f:
f.writelines(result)
```
注意,这段代码仅适用于去重以数字开头,后面跟着一个句点和6个单词的短语。如果您想要去重其他类型的内容,需要根据实际情况修改正则表达式。
import docx2txt import re # 读取docx文件内容 text = docx2txt.process("example.docx") # 使用正则表达式去重 pattern = r'^(?:(\d+).\s)?―.\n.?[答案]\s*(\w+)\b.*$' # 将去重后的内容写入新的文本文件 with open("example_output.txt", "w", encoding="utf-8") as f: f.write('\n'.join(result.keys()))
这段代码中使用了正则表达式去重,但是没有将匹配到的结果保存下来。可以在代码中添加如下部分,将匹配到的结果保存到一个字典中:
import docx2txt
import re
# 读取docx文件内容
text = docx2txt.process("example.docx")
# 使用正则表达式去重
pattern = r'^(?:(\d+).\s)?―.\n.?[答案]\s*(\w+)\b.*$'
result = {}
for line in text.split('\n'):
match = re.match(pattern, line)
if match:
q_num = match.group(1)
answer = match.group(2)
result[q_num] = answer
# 将去重后的内容写入新的文本文件
with open("example_output.txt", "w", encoding="utf-8") as f:
f.write('\n'.join(result.keys()))