根据您提供的信息,以下是您提到的关键信息列表:
1. 输入的断问答章节和部分的数量为:5个问题一节、100节一部分。
2. 使用json合并目录文件。
3. 要去重json数据。
4. 问题和答案将被转换为txt格式。
5. 默认情况下,章节末尾会加上时间戳(2023/4/5 3:16:17)。
- 如果代码出错,则不包含时间戳。
6. 输入使用的文件目录。
- 默认:读取JSON的路径为`/storage/emulated/0/下载/Document/`。
- 默认:合并JSON文件、去重JSON文件和保存TXT文件的路径为`/storage/emulated/0/0_培音/z`。
7. 您正在使用安卓终端Temux执行。
对于这些情况,代码需要进行相应调整以满足您的要求。如果还有其他问题,请随时提问。
根据您的要求和提供的信息,以下是相应调整过的代码:
```python
import os
import json
# 从输入获取问答章节和部分数量
section_size, part_size = map(int, input("请输入断问答章节和部分的数量(以空格分隔):").split())
# 默认值
timestamp_format = "2023/4/5 3:16:17"
input_directory = "/storage/emulated/0/下载/Document/"
output_directory = "/storage/emulated/0/0_培音/z"
# 合并所有 JSON 文件
merged_data = []
for file_name in os.listdir(input_directory):
if file_name.endswith(".json"):
with open(os.path.join(input_directory, file_name), "r", encoding="utf-8") as f:
data = json.load(f)
merged_data.extend(data)
# 去重 JSON 数据
unique_data = list({json.dumps(d, ensure_ascii=False) for d in merged_data})
unique_data = [json.loads(d) for d in unique_data]
# 按照设定的章节大小和部分大小进行分组
sections = []
if len(unique_data) > 0:
for i in range(0, len(unique_data), section_size):
section = unique_data[i:i + section_size]
sections.append(section)
parts = []
if len(sections) > 0:
for i in range(0, len(sections), part_size):
part = sections[i:i + part_size]
parts.append(part)
# 创建输出文件目录
os.makedirs(output_directory, exist_ok=True)
# 将结果输出到文件
if len(parts) > 0:
for part_index, part in enumerate(parts):
output = ""
for section_index, section in enumerate(part):
output += f"第{section_index + 1}节:\n"
for item in section:
question = item["question"]
answer = item["answer"]
output += f"[问题] {question}\n"
output += f"[答案] {answer}\n"
output += f"章节末尾时间戳:{timestamp_format}\n\n"
output_file = os.path.join(output_directory, f"output_part{part_index + 1}.txt")
with open(output_file, "w", encoding="utf-8") as f:
f.write(output)
else:
print("没有可输出的内容。")
json合并目录文件 合并json转txt
去重json
问题 答案
json转txt
每5个问题 一章节
每100章一部分
具体代码输入为
input= 请输入断问答章节 部分
5 100
表示 5个问答为一节 100节为一部分
[问题]
[答案]
默认:章节末尾时间戳:2023/4/5 3:16:17
如果代码出错就不加上
默认
input读取 文件目录
input存储问答章节 #假定输入5 100
默认:读取json路径/storage/emulated/0/下载/Document/
默认:保存合并json文件,去重json文件,保存txt文件路径
/storage/emulated/0/0_培音/z
我是安卓 temux的
你可以把各种情况全部算在一起吗?
我也是安卓手机run *py。
您更改您的目录为——
input_directory = "/sdcard/下载/Document/"
output_directory = "/sdcard/0_培音/z"
试试。
我手机存储卡的根目录是/sdcard/
我的qpython 目录绝对路径是/sdcard/qpython/
我的文档目录是/sdcard/Documents/
import json
dict_data = {
'English_name':'Jack',
'Chinese_name':'接客',
'age':18
}
with open('test.json','w',encoding='utf-8') as f:
f.write(json.dumps(dict_data,indent=4,ensure_ascii=False))
运行之后json
文件为:
{
"English_name": "Jack",
"Chinese_name": "接客",
"age": 18
}