简体   繁体   English

多个列表保存在单个文本文件中

[英]Multiple lists saved in a single text file

I have a text file with the following info (each condition in a separate line): 我有一个带有以下信息的文本文件(每个条件在一个单独的行中):

UP_aic up 920.5 4 17280.0 down 17764.5 2 28186.5 up 28249.1  
DOWN_aic down 941.0 2 8800.5 up 8894.3 down 11691.0 2 20316.2 up
20363.1 4 26901.8 down 26901.8  
UP_adc down 1477.1   
DOWN_adc up 1752.8

I have implemented the code to remove 2s and 4s and their respective timings (see below) and all I want is to resave this info in another text file! 我已经实现了删除2s和4s的代码以及它们各自的时间(见下文),我想要的是在另一个文本文件中重新保存这些信息!

However, out of all 15 (or so) attempts this morning I only managed to save the last line ( DOWN_adc up 1752.8 ) in various ways: normal, vertical instead of horizontal, all characters "glued" together etc. etc. 然而,在今天早上的所有15次(或左右)尝试中,我只设法以各种方式保存最后一行( DOWN_adc up 1752.8 ):正常,垂直而不是水平,所有字符“粘合”在一起等。等等。

So I kept the most basic write method here now. 所以我现在保留了最基本的写法。 I understand that all the previous lines get deleted by the next line so only the last one stays, but I can't figure out how to prevent this. 我知道所有以前的行都被下一行删除,所以只留下最后一行,但我无法弄清楚如何防止这种情况。

Here is the code: 这是代码:

from sys import argv
from itertools import tee, islice, chain, izip
script, from_file, to_file = argv 
print "Here's your file %r:" %from_file

fhand=open(from_file)
total = 0
for line in fhand:  
    words=line.split()
    def previous_and_next(some_iterable):
        items, nexts = tee(some_iterable, 2)
        nexts = chain(islice(nexts, 1, None), [None])
        return izip(items, nexts)
    for item, nxt in previous_and_next(words):
        if item=='2': 
           words.remove(item)
           words.remove(nxt)
        elif item=='4':
            words.remove(item)
            words.remove(nxt)
    print words

with open(to_file, 'w') as output:          
      output.write(str(words)+'\n')

fhand.close()
output.close() 

So, how do I save the data like this again each condition in a separate line (angle brackets, commas etc. are not a problem)? 那么,如何将这样的数据再次保存在一个单独的行中(尖括号,逗号等不是问题)?

['UP_aic', 'up', '920.5', 'down', '17764.5', 'up', '28249.1']  
['DOWN_aic', 'down', '941.0', 'up', '8894.3', 'down', '11691.0', 'up', '20363.1', 'down', '26901.8'] 
['UP_adc', 'down', '1477.1']  
['DOWN_adc', 'up', '1752.8'] 

The call to write() is outside of the for loop, therefore words is only written to the file after the loop has ended. write()的调用在for循环之外,因此在循环结束后只将words写入文件。 By that point, it contains whatever was in the last line read. 到那时,它包含最后一行读取的内容。

Change your code to something like 将您的代码更改为类似的内容

def previous_and_next(some_iterable):
    items, nexts = tee(some_iterable, 2)
    nexts = chain(islice(nexts, 1, None), [None])
    return izip(items, nexts)

with open(to_file, 'w') as output:      
    for line in fhand:  
        words = line.split()
        for item, nxt in previous_and_next(words):
            if item == '2': 
                words.remove(item)
                words.remove(nxt)
            elif item == '4':
                words.remove(item)
                words.remove(nxt)
        print words
        output.write(str(words)+'\n')

No need to call output.close() , that's what the with block is for. 无需调用output.close() ,这就是with block的用途。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM