0

如何复制preset5并将其粘贴到preset0?然后编辑预设0 -0.30 到 0.30 ?

import os

file_withlines = """
cat >> /tmp/test.log << \EOF
junks====================================1
otherjunks===============================0
preset0=0,2,-0.30,4
preset1=1\n,2,-0.30,4
preset2=2,2,-0.30\n,4
preset3 =3,2,-0.30,4
preset4= 4,2,-0.30,4
preset5 =5,0.2\n,-0.30,4
junksS==\n\r\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\r\r\r\r\r\rHere!\n\r
EOF
"""
os.system(file_withlines)
os.system("cat /tmp/test.log")

def read_log(findme):
  f = open('/tmp/test.log', "r")
  for line in f:
    if "=" in line:
      key,val = line.split("=",1)
      if findme in key:
        return val.replace("\\n", "").rstrip()

def copy_or_edit_line():
  # a = os.popen("grep preset5 /tmp/test.log | head -1").read()
  # sed -i s/"^slave.pcm \"hw:.*"/"slave.pcm \"hw:${out}\""/ /etc/asound.conf
  print "how!"


# 1) Read
print read_log("preset5")

# 2) Copy preset5 and paste to preset0

print read_log("preset0")

# 3) Edit preset0 values 0,2,-0.30,4 to 0,2,0.30,4

print read_log("preset0")
4

1 回答 1

0

临时锻炼。

import os

def read_log(findme):
  f = open('/var/tmp/a.ini', "r")
  for line in f:
    if "=" in line:
      key,val = line.split("=",1)
      if findme in key:
        return val.replace("\\n", "").rstrip()

def replace_preset(findme):
  a = os.popen("grep %s /var/tmp/a.ini" % findme).read()
  a=  a.replace("%s=" % findme, "").replace("\\n", "").rstrip()
  os.system('sed -i s/"^preset0=.*"/"preset0=' + a + '"/ /var/tmp/a.ini')

def replace_preset_byfield(input, reflace, reflaceby):
  try:
    input = input.replace("\\n", "").rstrip()
    input = input.split(",")
    input[reflace] = reflaceby.replace("\\n", "").rstrip()
    print input
    a  = "%s,%s,%s,%s" % (input[0],input[1],input[2],input[3])
    os.system('sed -i s/"^preset0=.*"/"preset0=' + a + '"/ /var/tmp/a.ini')
  except Exception, err:
    print err
    pass

# 1) Read
print read_log("preset5")

# 2) Copy preset5 and paste to preset0
replace_preset("preset5")
print read_log("preset0")

# 3) Edit preset0 values
# replace_preset_byfield(read_log("preset0"), 0, 55 )
replace_preset_byfield(read_log("preset0"), 0, "55" )
print read_log("preset0")
于 2013-10-18T03:40:54.020 回答