alsamixer 使用以下语法将其数据存储在文件中:
control.2 = {
iface : MIXER
name : 'DSP1 Rate'
value:'16kHz'
comment = {
access 'read write'
type ENUMERATED
count 1
item.0 'SYNCCLK rate'
item.1 '8kHz'
item.2 '16kHz'
item.3 'ASYNCCLK rate'
}
有没有一种快速的方法可以将其转换为Python字典? 允许从 python 脚本中读取它?
我尝试使用 pyalsaaudio 和 alsaaudio.mixers()
可以看到控件的名称,但是使用 alsa 文件,您还可以看到可用的控件您可以为该控件设置的数据。
最佳答案
我花了整个下午的时间来做这件事。希望它能解决您的问题:
#!/usr/bin/env python2.7
import re
import json
def jsonify(config='/var/lib/alsa/asound.state'):
g = re.MULTILINE
p1 = re.compile(r'((?:^|"[^"]+"|\'[^\']+\'|[\s:{}])+)([^\s{}]+)', g)
p2 = re.compile(r'^(\s*\"[\w.]+"(?!\s*[\:]))', g)
p3 = re.compile(r'([\"\}])\n(?!\s*\})', g)
with open(config) as fh:
context = fh.read()
# replace all the quote with quotes
context = context.replace("'", '"')
# surround the name:value objects by quotes
context = re.sub(p1, r'\g<1>"\g<2>"', context)
# add a trailing comma after the values assignment
context = re.sub(p2, r'\g<1>:', context)
# add a colon before the values as a delimiter
context = re.sub(p3, r'\g<1>,\n', context)
return json.loads('{\n%s\n}' % context[:-2])
if __name__ == '__main__':
print 'you should use it as module :D'
关于python - alsamixer amixer 转 python 字典格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31936091/