帮我写一个python,读取上一级目录中的1.txt文件,判断其中是否包含a-b-c字段,若包含,打印ok,若不包含,打印fail
时间: 2024-03-16 15:44:51 浏览: 45
好的,可以使用下面的python代码实现:
```python
import os
# 获取上一级目录路径
parent_dir = os.path.abspath(os.path.join(os.getcwd(), ".."))
# 读取1.txt文件
with open(os.path.join(parent_dir, "1.txt"), "r") as f:
content = f.read()
# 判断是否包含a-b-c字段
if "a-b-c" in content:
print("ok")
else:
print("fail")
```
注意,这里使用了os模块获取上一级目录路径,然后使用os.path.join拼接出1.txt文件的路径。最后使用with语句读取文件内容并判断是否包含指定字段。
相关问题
windows系统中python读取文件1.txt 提取出文件中指定字段生成word表格
可以使用Python的`docx`库来生成Word表格,使用`re`库来匹配文件中的字段。具体实现步骤如下:
1. 安装`python-docx`库和`re`库:在命令行窗口输入以下命令:
```
pip install python-docx
pip install re
```
2. 导入需要的库:
```python
import docx
import re
```
3. 打开文件并读取内容:
```python
with open('1.txt', 'r', encoding='utf-8') as f:
content = f.read()
```
4. 使用正则表达式匹配需要提取的字段,例如匹配姓名和年龄:
```python
name_pattern = r'姓名:(\w+)'
age_pattern = r'年龄:(\d+)'
name = re.search(name_pattern, content).group(1)
age = re.search(age_pattern, content).group(1)
```
5. 创建Word文档并添加表格:
```python
doc = docx.Document()
table = doc.add_table(rows=2, cols=2)
table.cell(0, 0).text = '姓名'
table.cell(0, 1).text = '年龄'
table.cell(1, 0).text = name
table.cell(1, 1).text = age
```
6. 保存Word文档:
```python
doc.save('output.docx')
```
完整代码如下:
```python
import docx
import re
with open('1.txt', 'r', encoding='utf-8') as f:
content = f.read()
name_pattern = r'姓名:(\w+)'
age_pattern = r'年龄:(\d+)'
name = re.search(name_pattern, content).group(1)
age = re.search(age_pattern, content).group(1)
doc = docx.Document()
table = doc.add_table(rows=2, cols=2)
table.cell(0, 0).text = '姓名'
table.cell(0, 1).text = '年龄'
table.cell(1, 0).text = name
table.cell(1, 1).text = age
doc.save('output.docx')
```
使用python写一个程序:配置文件set.ini,读取set.ini中的module字段,判断每个module=0的类型,并执行同目录下的与module名称相同的py文件
好的,以下是代码实现:
```python
import configparser
import importlib.util
import os
# 读取配置文件
config = configparser.ConfigParser()
config.read('set.ini')
# 遍历所有section
for section in config.sections():
# 获取module字段的值
module_value = config.get(section, 'module')
# 判断module是否为0
if module_value == '0':
# 获取同名的py文件路径
module_path = os.path.join(os.path.dirname(__file__), f'{section}.py')
# 判断文件是否存在
if os.path.exists(module_path):
# 动态导入模块
module_spec = importlib.util.spec_from_file_location(section, module_path)
module = importlib.util.module_from_spec(module_spec)
module_spec.loader.exec_module(module)
else:
print(f'{section}.py文件不存在')
```
其中,`set.ini`文件的格式如下:
```
[section1]
module = 1
[section2]
module = 0
[section3]
module = 0
```
其中,`module`字段为0表示需要执行同名的py文件,为1表示不需要执行。程序会遍历所有的section,判断module字段是否为0,如果是,则会动态导入同名的py文件并执行。如果同名的py文件不存在,则会输出错误提示。
阅读全文
相关推荐
![7z](https://img-home.csdnimg.cn/images/20241231044736.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.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)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)