我有一个函数,其中转换是一个列表,文件名是输入文件的名称。我想在一行中写入 5 个字符,然后添加新行并在该行中添加 5 个字符并添加新行,直到从列表转换为文件文件名没有任何可写的内容。我该怎么做?
根据我对你的问题的理解,你可能需要如下所示的代码:
import os
def write_data_file(your_collection, data_file_path):
"""Writes data file from memory to previously specified path."""
the_string = ''
for row in your_collection:
for i, c in enumerate(row):
if i % 5 < 4:
the_string += c
else:
the_string += os.linesep + c
with open(data_file_path, 'w') as df:
df.write(the_string)
my_collection = [
"This is more than five characters",
"This is definately more than five characters",
"NOT",
"NADA"
]
write_data_file(my_collection, '/your/file/path.txt')
除此之外,您可能需要澄清您在问什么。此代码片段循环遍历集合(如列表),然后循环访问集合中该行中包含的假定字符串。每当达到 5 个字符的限制时,它都会添加新行。
def foo(conversion, filename):
with open(filename, "a") as f:
line = ""
for s in conversion:
for c in s:
if len(line) < 5:
line += c
else:
f.write(line + "n")
line = c
f.write(line)
将字符串列表转换为一个大字符串,然后一次遍历该字符串五个字符,并在每次循环迭代后插入""。然后将其写入文件。这是我的意思的一个基本示例,可能需要一些调整,但它会给你一个想法:
# concatenate all the strings for simplicity
big_string = ""
for single_string in conversion:
big_string += single_string
# loop through using 5 characters at a time
string_with_newlines = ""
done = False
while not done:
next_five_chars = big_string[:5]
big_string = big_string[5:]
if next_five_chars:
string_with_newlines += next_five_chars + "n"
else:
done = True
# write it all to file
with open(filename, "w") as your_file:
your_file.write(string_with_newlines)
l = ["one", "two", "three", "four", "five"]
def write(conversion, filename):
string = "".join(conversion)
f = open(filename, "a")
for i in range(5, len(string) + 5, 5):
f.write(string[i-5:i])
f.write("n")
if __name__ == '__main__':
write(l, "test.txt")
这将创建一个名为"test.txt"的文件,其中包含以下内容:
onetw
othre
efour
onetw
othre
efour
five
我做了一个函数来分组你的列表:
import itertools
def group(iterable, n):
gen = (char for substr in iterable for char in substr)
while True:
part = ''.join(itertools.islice(gen, 0, n))
if not part:
break
yield part
介绍:
>>> l = ['DBTsiQoECGPPo', 'd', 'aDAuehlM', 'FbUnSuMLuEbHe', 'jRvARVZMn', 'SbGCi'
, 'jhI', 'Rpbd', 'uspffRvPiAmbQEoZDFAG', 'RIbHAcbREdqpMDX', 'bqVMrN', 'FtU', 'nu
fWcfjfmAaUtYtwNUBc', 'oZvk', 'EaytqdRkICuxqbPaPulCZlD', 'dVrZdidLeakPT', 'qttRfH
eJJMOlJRMKBM', 'SAiBrdPblHtRGpjpZKuFLGza', 'RxrLgclVavoCmPkhR', 'YuulTYaNTLghUkK
riOicMuUD']
>>> list(group(l, 5))
['DBTsi', 'QoECG', 'PPoda', 'DAueh', 'lMFbU', 'nSuML', 'uEbHe', 'jRvAR', 'VZMnS'
, 'bGCij', 'hIRpb', 'duspf', 'fRvPi', 'AmbQE', 'oZDFA', 'GRIbH', 'AcbRE', 'dqpMD
', 'XbqVM', 'rNFtU', 'nufWc', 'fjfmA', 'aUtYt', 'wNUBc', 'oZvkE', 'aytqd', 'RkIC
u', 'xqbPa', 'PulCZ', 'lDdVr', 'ZdidL', 'eakPT', 'qttRf', 'HeJJM', 'OlJRM', 'KBM
SA', 'iBrdP', 'blHtR', 'GpjpZ', 'KuFLG', 'zaRxr', 'LgclV', 'avoCm', 'PkhRY', 'uu
lTY', 'aNTLg', 'hUkKr', 'iOicM', 'uUD']
>>> 'n'.join(group(l, 5))
'DBTsinQoECGnPPodanDAuehnlMFbUnnSuMLnuEbHenjRvARnVZMnSnbGCijnhIRpbndu
spfnfRvPinAmbQEnoZDFAnGRIbHnAcbREndqpMDnXbqVMnrNFtUnnufWcnfjfmAnaUtYt
nwNUBcnoZvkEnaytqdnRkICunxqbPanPulCZnlDdVrnZdidLneakPTnqttRfnHeJJMnO
lJRMnKBMSAniBrdPnblHtRnGpjpZnKuFLGnzaRxrnLgclVnavoCmnPkhRYnuulTYnaNTL
gnhUkKrniOicMnuUD'
将'n'.join(group(l, 5))
的结果写入文件。