2014-04-01 61 views
0

我有一個功能,其中轉換是一個列表和文件名是輸入文件的名稱。我想在一行中寫入5個字符,然後添加一個新行並將5個字符添加到該行,並添加一個新行,直到沒有任何內容從列表轉換寫入文件文件名爲止。我怎樣才能做到這一點?如何在Python中將新行寫入文件?

+0

你需要的是建立一個字符串,並添加'「\ n」'你想每次新隊。爲了澄清,「轉換」是什麼樣的列表?這是一個字符串列表嗎?字符列表? –

+0

它是一個字符串列表。 – user3459811

回答

0

將字符串列表轉換爲一個大字符串,然後一次循環該字符串中的五個字符,並在每次迭代循環後插入「\ n」。然後將其寫入文件。這裏是什麼,我的意思是一個基本的例子,可能需要一些調整,但它會給你的想法:

# 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) 
0

從我想我明白你的問題,你可能需要的代碼看起來像這樣:

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個字符限制,它就會添加新行。

0
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) 
0

我做了一個功能分組列表:

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)) 
'DBTsi\nQoECG\nPPoda\nDAueh\nlMFbU\nnSuML\nuEbHe\njRvAR\nVZMnS\nbGCij\nhIRpb\ndu 
spf\nfRvPi\nAmbQE\noZDFA\nGRIbH\nAcbRE\ndqpMD\nXbqVM\nrNFtU\nnufWc\nfjfmA\naUtYt 
\nwNUBc\noZvkE\naytqd\nRkICu\nxqbPa\nPulCZ\nlDdVr\nZdidL\neakPT\nqttRf\nHeJJM\nO 
lJRM\nKBMSA\niBrdP\nblHtR\nGpjpZ\nKuFLG\nzaRxr\nLgclV\navoCm\nPkhRY\nuulTY\naNTL 
g\nhUkKr\niOicM\nuUD' 

寫的'\n'.join(group(l, 5))結果到一個文件中。

0
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