我的代码:
遇到的问题:
写入结果全部保存在每个txt中
正确实现结果展示:
...
思路:是否可以根据列表的索引属性进行对应处理,或是更好的思路解决
建议 试试编程必练___习题:
请在控制台打印如下图案:
*****
****
***
**
*
names = ['文件{}'.format(n) for n in range(1, 11)]
values = ['{}数据{}\n'.format(n, n) for n in range(1, 11)]
def write_center(filename, center):
with open(filename, 'w') as f:
f.write(center)
f.write('{}\n{}\n'.format('二', '这是第三行数据'))
for f, c in zip(names, values):
write_center(f + '.txt', c)
import os
def answer_create(path):
for file in os.listdir(path):
# print(file)
if os.path.splitext(file)[1] == ".jpg" or os.path.splitext(file)[1] == ".jpeg":
print(file)
answer_data = file
car_answer = answer_data.split('_')
print(len(car_answer))
print(car_answer[0])
print(car_answer[1])
print(car_answer[2])
print(car_answer[3])
print(car_answer[4].split('.')[1])
print(car_answer[5])
# other...
# 答案取值
finally_answer = car_answer[3]
# 类型
blue_data = "\n蓝\n"
green_data = "\n绿\n"
yellow_green_data = "\n黄绿\n"
yellow_data1 = "\n黄\n"
yellow_data2 = "\n黄\n"
white_data = "\n白1\n"
white_data2 = "\n白2\n"
white_data3 = "\n白3\n"
black_data = "\n黑\n"
green_data2 = "\n绿\n"
# 拼接答案txt
answer_file = file.split(".")[0] + ".txt"
print(answer_file)
answer_path = os.path.join("H:\\file", answer_file)
print(answer_path)
create_answer = open(answer_path, 'a')
# 写入answer
create_answer.write(finally_answer + green_data)
answer_create("H:\file")
print("----Complete----")
这是数据提取和文件分离吧
– 小码果 2年前