[英]How to extract values which is match some conditions from list in Python?
[英]Extract data from list with some conditions
基本上,我試圖添加兩個midi文件,並且互聯網上沒有太多有關此文件的信息,所以我正在嘗試自己的文件。
到目前為止,我所做的是添加了兩個midi消息(midi數據的類型),並且列出了兩個midi消息。 這意味着我現在擁有所有需要合並兩個Midi的數據。 問題是我無法以特定格式添加數據。
所以我的代碼是:
from mido import MidiFile, MidiTrack
mid = MidiFile('har.mid')
mid2 = MidiFile('har2.mid')
l = [msg for track in mid.tracks for msg in track]
l.pop()
ka = [msg for track in mid2.tracks for msg in track]
ka.pop()
result = l + ka
for messagess in result:
print(messagess)
我得到這個輸出:
note_on channel=0 note=59 velocity=40 time=0
note_on channel=0 note=60 velocity=40 time=0
note_on channel=0 note=64 velocity=40 time=0
note_off channel=0 note=59 velocity=0 time=55
note_off channel=0 note=64 velocity=0 time=0
note_on channel=0 note=52 velocity=40 time=0
note_off channel=0 note=60 velocity=0 time=55
note_on channel=0 note=64 velocity=40 time=0
note_on channel=0 note=67 velocity=40 time=0
note_off channel=0 note=67 velocity=0 time=55
note_on channel=0 note=57 velocity=40 time=0
note_off channel=0 note=52 velocity=0 time=55
note_off channel=0 note=57 velocity=0 time=0
note_off channel=0 note=64 velocity=0 time=0
note_on channel=0 note=57 velocity=40 time=55
note_off channel=0 note=57 velocity=0 time=55
note_on channel=0 note=64 velocity=40 time=0
note_on channel=0 note=67 velocity=40 time=0
note_off channel=0 note=64 velocity=0 time=55
note_off channel=0 note=67 velocity=0 time=0
note_on channel=0 note=57 velocity=40 time=110
note_on channel=0 note=64 velocity=40 time=0
note_off channel=0 note=57 velocity=0 time=55
note_off channel=0 note=64 velocity=0 time=0
note_on channel=0 note=62 velocity=40 time=0
note_off channel=0 note=62 velocity=0 time=55
note_on channel=0 note=57 velocity=40 time=110
note_on channel=0 note=62 velocity=40 time=0
note_off channel=0 note=57 velocity=0 time=55
note_off channel=0 note=62 velocity=0 time=0
note_on channel=0 note=60 velocity=40 time=0
note_on channel=0 note=62 velocity=40 time=0
note_on channel=0 note=67 velocity=40 time=0
note_on channel=0 note=60 velocity=40 time=55
note_on channel=0 note=64 velocity=40 time=0
note_off channel=0 note=60 velocity=0 time=55
note_off channel=0 note=62 velocity=0 time=0
note_off channel=0 note=64 velocity=0 time=0
note_off channel=0 note=67 velocity=0 time=55
note_on channel=0 note=64 velocity=40 time=0
note_off channel=0 note=64 velocity=0 time=55
note_on channel=0 note=60 velocity=40 time=0
note_off channel=0 note=60 velocity=0 time=55
note_on channel=0 note=62 velocity=40 time=0
note_off channel=0 note=62 velocity=0 time=55
note_on channel=0 note=64 velocity=40 time=0
note_off channel=0 note=64 velocity=0 time=55
note_on channel=0 note=60 velocity=40 time=110
note_on channel=0 note=62 velocity=40 time=0
note_off channel=0 note=60 velocity=0 time=55
note_off channel=0 note=62 velocity=0 time=0
note_on channel=0 note=48 velocity=40 time=110
note_on channel=0 note=62 velocity=40 time=0
note_off channel=0 note=48 velocity=0 time=55
note_off channel=0 note=62 velocity=0 time=0
note_on channel=0 note=57 velocity=40 time=55
note_on channel=0 note=60 velocity=40 time=0
from mido import Message, MidiFile, MidiTrack
mid = MidiFile()
track = MidiTrack()
mid.tracks.append(track)
track.append(Message('program_change', program=12, time=0))
track.append(Message('note_on', note=64, velocity=64, time=32))
track.append(Message('note_off', note=64, velocity=127, time=32))
mid.save('new_song.mid')
所以我的問題是如何從這種格式追加每一行:
note_off channel=0 note=62 velocity=0 time=0
此格式
'note_off', note=62, velocity=0, time=0
在track.append(Message())
因為track.append僅支持以下格式方法:
track.append(Message('note_off', note=62, velocity=0, time=0))
提前致謝。
from mido import MidiFile, MidiTrack
mid = MidiFile('har.mid')
mid2 = MidiFile('har2.mid')
l = [msg for track in mid.tracks for msg in track]
l.pop()
ka = [msg for track in mid2.tracks for msg in track]
ka.pop()
result = l + ka
mid3 = MidiFile()
track = MidiTrack()
mid3.tracks.append(track)
for m in result:
track.append(m)
mid3.save('new_song.mid')
result
的對象應該已經是Message
對象,因此您無需再次構造它們。 嘗試使用此代碼,如果它不起作用,請復制完整的錯誤消息,您會回來。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.