No=re.search(r'\d{18}',i) number.append(No.group())
时间: 2024-06-04 17:08:42 浏览: 5
这段代码的作用是在字符串 `i` 中查找18位连续数字的模式,并将找到的结果保存在 `No` 对象中。然后,如果找到了匹配的模式,就将该模式添加到 `number` 列表中。其中,`\d` 表示匹配数字字符,`{18}` 表示匹配18次,即匹配18位连续数字。如果没有找到匹配的模式,`No.group()` 将会报错,因为 `No` 对象不存在 `group()` 方法。
相关问题
import sys import re import collections WORD_RE = re.compile(r'\w+') index = collections.defaultdict(list) with open(sys.argv[1], encoding='utf-8') as fp: for line_no, line in enumerate(fp, 1): for match in WORD_RE.finditer(line): word = match.group() column_no = match.start()+1 location = (line_no, column_no) index[word].append(location) for word in sorted(index, key=str.upper): print(word, index[word]) Traceback (most recent call last): File "C:\Users\YJanu\yuanzu.py", line 5, in <module> for line_no, line in enumerate(fp, 1): NameError: name 'fp' is not defined 报错,修改代码可以正确执行
这个错误是因为 `fp` 变量没有被正确定义,可能是因为文件名参数没有传递给脚本。你可以尝试修改代码,在脚本执行时传递文件名参数,例如:
```python
import sys
import re
import collections
WORD_RE = re.compile(r'\w+')
index = collections.defaultdict(list)
with open(sys.argv[1], encoding='utf-8') as fp:
for line_no, line in enumerate(fp, 1):
for match in WORD_RE.finditer(line):
word = match.group()
column_no = match.start()+1
location = (line_no, column_no)
index[word].append(location)
for word in sorted(index, key=str.upper):
print(word, index[word])
```
在命令行中执行该脚本时,需要传递一个文件名参数,例如:
```
python script.py mytextfile.txt
```
其中 `script.py` 是你的脚本文件名,`mytextfile.txt` 是你要处理的文本文件名。
import os import re def get_toc(html): start_url='https://www.kanunu8.com/book3/6879/' toc_url_list=[] toc_block=re.findall('正文(.*?)</tbody>',html,re.S)[0] toc_url=re.findall(' href="(.*?)"',toc_block,re.S) for url in toc_url: toc_url_list.append(start_url+url) return toc_url_list def get_article(html): chapter_name=re.search('size="4">(.*?)<',html,re.S).group(1) text_block=re.search('<p>(.*?)</p>',html,re.S).group(1) text_block=text_block.replace('<br />','') return chapter_name,text_block def save(chapter,article): os.makedirs('F:\植物大战僵尸',exist_ok=True) with open(os.path.join('F:\植物大战僵尸',chapter+'.txt'),'w',encoding='utf-8')as f: f.write(article) 章节内容无法保存到对应路径
根据你提供的代码,章节内容保存到对应路径的问题可能是由于文件路径的问题引起的。你在 `save` 函数中使用了硬编码的文件路径 `'F:\植物大战僵尸'`,这可能导致无法在某些操作系统上正常保存文件。
你可以尝试使用相对路径或者通过用户输入来获取文件保存路径。下面是一个示例,演示如何通过用户输入来获取文件保存路径:
```python
import os
import re
def get_toc(html):
start_url='https://www.kanunu8.com/book3/6879/'
toc_url_list=[]
toc_block=re.findall('正文(.*?)</tbody>',html,re.S)[0]
toc_url=re.findall(' href="(.*?)"',toc_block,re.S)
for url in toc_url:
toc_url_list.append(start_url+url)
return toc_url_list
def get_article(html):
chapter_name=re.search('size="4">(.*?)<',html,re.S).group(1)
text_block=re.search('<p>(.*?)</p>',html,re.S).group(1)
text_block=text_block.replace('<br />','')
return chapter_name,text_block
def save(chapter, article):
folder_path = input("请输入文件夹路径:")
os.makedirs(folder_path, exist_ok=True)
file_path = os.path.join(folder_path, chapter + '.txt')
with open(file_path, 'w', encoding='utf-8') as f:
f.write(article)
# 使用示例
chapter_name = "第一章"
article = "这是第一章的内容"
save(chapter_name, article)
```
在这个示例中,`save` 函数通过用户输入来获取文件夹路径,并将章节内容保存到该路径下的以章节名命名的文件中。
请根据你的实际需求调整代码,并确保文件路径的正确性。如果你有任何其他问题,请随时提问。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)