Readin a .txt file and put the elements in a list (Python)

前端 未结 3 1007
南旧
南旧 2021-01-25 05:52

Im new in Python and i need some help. i got a .txt file in this form:

Time[tab]Signal

0[tab]1.05

0.5[tab]1.06

1[tab]1.09

1.5[tab]1.12

Now

3条回答
  •  轮回少年
    2021-01-25 05:56

    I have created a .txt file with same contents that you provided and the program you wrote prints this (multi-dimensional list):

    [['Time', 'Signal'], ['0.5', '1.06'], ['1', '1.09'], ['1.5', '1.12']]
    

    By the looks of it your file isn't actually .txt.

    To answer your question:

    del list1[1]
    

    This will delete the wrong position. To delete Time/Signal line, you should do this instead:

    del list1[0]
    

    This is because items in Python lists start at positon 0.

    Try this code (list comprehension):

    daten = open("extedit.txt", "r")
    lines = daten.readlines()
    list1 = []
    for i in lines:
        list1.append(i.strip().split('\t'));
    daten.close()
    
    del list1[0]
    
    zeit = [i[0] for i in list1]
    
    signal = [i[1] for i in list1]
    
    print(zeit)
    print(signal)
    

提交回复
热议问题