如何读取数字组合文件并将每个组合保存为新文件名?
how to read a file of combination of numbers and save saving each combination into a new file name?
我有一个包含特定组合数字的组合文件,例如:
(133, 134), (133, 100), (133, 438), (133, 414), (133, 35), (133, 130), (133, 143), (133, 383), (134, 100), (134, 438), (134, 414), (134, 35), (134, 130), (134, 143), (134, 383), (100, 438) , (100, 414), (100, 35), (100, 130), (100, 143), (100, 383), (438, 414), (438, 35), (438, 130), ( 438, 143), (438, 383), (414, 35), (414, 130), (414, 143), (414, 383), (35, 130), (35, 143), (35, 383), (130, 143), (130, 383), (143, 383)
我想将每个组合保存到一个新文件名中,并在每个数字后添加字符串 "A"。
我已经尝试过逐个字符地阅读,但它不起作用。
import functools
with open ("combi2.txt", "r+") as f:
f_read_ch=functools.partial(f.read,3)
for ch in iter (f_read_ch,''):
print(repr(ch))
Combi_1.txt
第133回
第134话
Combi_2.txt
第133回
100安
Combi_3.txt
第133回
第134话
给你:
import re
# collect data
raw_data = []
with open('combi2.txt', 'r') as file:
data = file.readlines()
raw_data.append(data[0])
# find numbers sets in text
numbers_set = re.findall('(\d+,\s\d+)', raw_data[0])
# create files
count = 0
for item in numbers_set:
element = item.split(', ')
file = open('Combi_' + str(count) + '.txt', 'w')
file.write(element[0] + ' A ' + element[1] + ' A')
count += 1
输出示例:
Combi_0.txt with 133 A 134 A inside
我有一个包含特定组合数字的组合文件,例如:
(133, 134), (133, 100), (133, 438), (133, 414), (133, 35), (133, 130), (133, 143), (133, 383), (134, 100), (134, 438), (134, 414), (134, 35), (134, 130), (134, 143), (134, 383), (100, 438) , (100, 414), (100, 35), (100, 130), (100, 143), (100, 383), (438, 414), (438, 35), (438, 130), ( 438, 143), (438, 383), (414, 35), (414, 130), (414, 143), (414, 383), (35, 130), (35, 143), (35, 383), (130, 143), (130, 383), (143, 383)
我想将每个组合保存到一个新文件名中,并在每个数字后添加字符串 "A"。
我已经尝试过逐个字符地阅读,但它不起作用。
import functools
with open ("combi2.txt", "r+") as f:
f_read_ch=functools.partial(f.read,3)
for ch in iter (f_read_ch,''):
print(repr(ch))
Combi_1.txt 第133回 第134话
Combi_2.txt 第133回 100安
Combi_3.txt 第133回 第134话
给你:
import re
# collect data
raw_data = []
with open('combi2.txt', 'r') as file:
data = file.readlines()
raw_data.append(data[0])
# find numbers sets in text
numbers_set = re.findall('(\d+,\s\d+)', raw_data[0])
# create files
count = 0
for item in numbers_set:
element = item.split(', ')
file = open('Combi_' + str(count) + '.txt', 'w')
file.write(element[0] + ' A ' + element[1] + ' A')
count += 1
输出示例:
Combi_0.txt with 133 A 134 A inside