python将字典写入py
时间: 2023-12-20 19:06:05 浏览: 42
文件可以使用以下代码:
```python
import json
data = {"name": "John", "age": 30, "city": "New York"}
with open("data.py", "w") as f:
f.write("data = ")
json.dump(data, f)
```
这将创建一个名为"data.py"的文件,并将字典"data"写入该文件中。在写入字典之前,我们使用"f.write"函数将"data ="写入文件,这是为了在导入文件时创建正确的变量名。我们使用"json.dump"函数将字典写入文件。
相关问题
python密码字典生成器
Python密码字典生成器可以通过Python编写一个脚本来实现。以下是一个简单的示例:
```python
import itertools
import argparse
def generate_passwords(template_file, keywords, special_chars, output_file):
# 读取模板文件
with open(template_file, 'r') as f:
template = f.read().strip()
# 生成关键字排列组合
keyword_combinations = []
for i in range(1, len(keywords) + 1):
keyword_combinations += itertools.permutations(keywords, i)
# 生成密码
passwords = []
for combination in keyword_combinations:
password = template
for keyword in combination:
password = password.replace('{}', keyword, 1)
passwords.append(password)
# 添加特殊字符
if special_chars:
passwords_with_special_chars = []
for password in passwords:
for char_combination in itertools.product(special_chars, repeat=len(password)):
password_with_special_chars = ''
for i in range(len(password)):
password_with_special_chars += password[i] + char_combination[i]
passwords_with_special_chars.append(password_with_special_chars)
passwords = passwords_with_special_chars
# 写入文件
with open(output_file, 'w') as f:
for password in passwords:
f.write(password + '\n')
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Generate customized password dictionary.')
parser.add_argument('-t', '--template', required=True, help='Template file path.')
parser.add_argument('-k', '--keywords', required=True, help='Keywords separated by comma.')
parser.add_argument('-s', '--special-chars', help='Special characters separated by comma.')
parser.add_argument('-o', '--output', required=True, help='Output file path.')
args = parser.parse_args()
keywords = args.keywords.split(',')
special_chars = args.special_chars.split(',') if args.special_chars else None
generate_passwords(args.template, keywords, special_chars, args.output)
```
使用方法:
1. 创建一个模板文件,例如`template.txt`,其中包含一个或多个`{}`占位符,用于替换关键字。
2. 运行以下命令生成密码字典:
```
python password_generator.py -t template.txt -k keyword1,keyword2,keyword3 -s !,@,#,$ -o passwords.txt
```
其中,`-t`指定模板文件路径,`-k`指定关键字序列,以逗号分隔开,`-s`指定一些特殊符号,以逗号分隔开,`-o`指定输出后的文件名。
如果不需要特殊字符,可以省略`-s`参数。
生成的密码字典将保存在`passwords.txt`文件中。
python使用pyexcel写入内容到xlsx文件示例
下面是一个使用 pyexcel 写入内容到 xlsx 文件的示例:
```python
import pyexcel
# 创建一个字典列表
data = [
{"姓名": "张三", "年龄": 20, "性别": "男"},
{"姓名": "李四", "年龄": 22, "性别": "男"},
{"姓名": "王五", "年龄": 21, "性别": "女"}
]
# 将字典列表写入到 xlsx 文件中
pyexcel.save_as(records=data, dest_file_name="data.xlsx")
```
运行上述代码后,会在当前目录下生成一个名为 `data.xlsx` 的文件,其中包含了我们刚刚创建的字典列表的数据。
相关推荐
![](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)