Python: problème avec type de variable

Bonjour,

j’ai un petit problème avec ce code python:

niv = os.system("rec -n stat trim 0 .01 2>&1 | awk '/^Maximum amplitude/' | cut -d':' -f2 | sed 's/ //g'") print niv*100

Je pense que c’est parce que je récupère une valeur de type string et du coup la multiplication ne marche pas…
C’est peut-être tout con, mais je ne trouve pas comment faire… :slightly_smiling:

J’ai évidemment essayé float(os.system("…")) mais sans succès…

Merci!

Trouvé comme ça:

À la place d’os.system et os.popen, la doc recommande d’utiliser le module subprocess. J’utilise souvent subprocess.call pour appeler des commandes système.

Sinon, pour vérifier le type d’une variable, tu as la fonction embarquée type.

[code]>>> type(“foo”)
<class ‘str’>

type(2)
<class ‘int’>

type(2.0)
<class ‘float’>[/code]

Bonjour
J’utiliserai aussi le module re (regex) à la place de awk | cut | sed

Remarque même avec bash je n’utiliserai que sed (ne connaissant pas awk) avec une expression régulière.

Bonjour et merci de vos réponses!

En fait j’ai un autre soucis: la commande ci-dessus avec rec me prend trop de ressources (pulseaudio en permanence à 15-20%). J’ai une autre solution qui à l’air moins gourmande:

qui me renvoie quelque chose comme ça:

Capture WAVE '/dev/null' : Signed 16 bit Little Endian, Frquence 8000 Hz, Stro Avertissement: la frquence n'est pas prcise (demand = 8000Hz, obtenu = 44100Hz) veuillez essayez le greffon de branchement Hardware PCM card 0 'HDA Intel' device 0 subdevice 0 Its setup is: stream : CAPTURE access : RW_INTERLEAVED format : S16_LE subformat : STD channels : 2 rate : 44100 exact rate : 44100 (44100/1) msbits : 16 buffer_size : 16384 period_size : 4096 period_time : 92879 tstamp_mode : NONE period_step : 1 avail_min : 4096 period_event : 0 start_threshold : 1 stop_threshold : 16384 silence_threshold: 0 silence_size : 0 boundary : 1073741824 appl_ptr : 0 hw_ptr : 0 Pic max. (8192 chantillons): 0x00000071 # 0% Pic max. (8192 chantillons): 0x00000056 # 0% Pic max. (8192 chantillons): 0x00000060 # 0% Pic max. (8192 chantillons): 0x000000b7 # 0% Pic max. (8192 chantillons): 0x00000129 # 0% Pic max. (8192 chantillons): 0x000000de # 0% Pic max. (8192 chantillons): 0x00000047 # 0% Pic max. (8192 chantillons): 0x00000056 # 0% Pic max. (8192 chantillons): 0x00000095 # 0% Pic max. (8192 chantillons): 0x000000a1 # 0% Pic max. (8192 chantillons): 0x0000011b # 0% Pic max. (8192 chantillons): 0x000000a7 # 0% Pic max. (8192 chantillons): 0x0000016f # 1% Pic max. (8192 chantillons): 0x0000009a # 0% Pic max. (8192 chantillons): 0x00000074 # 0% Pic max. (8192 chantillons): 0x000000fb # 0% Pic max. (8192 chantillons): 0x00000051 # 0% Pic max. (8192 chantillons): 0x00000073 # 0% Pic max. (8192 chantillons): 0x00000088 # 0% Pic max. (8192 chantillons): 0x0000022d # 1% Pic max. (8192 chantillons): 0x00000126 # 0% Pic max. (8192 chantillons): 0x00000109 # 0% Pic max. (8192 chantillons): 0x00000211 # 1% Pic max. (8192 chantillons): 0x000001bf # 1% Pic max. (8192 chantillons): 0x00000048 # 0% Pic max. (8192 chantillons): 0x000000c4 # 0% Pic max. (8192 chantillons): 0x000000f5 # 0% Pic max. (8192 chantillons): 0x000003f9 # 3% Pic max. (8192 chantillons): 0x000003f3 # 3% Pic max. (8192 chantillons): 0x00000327 # 2% Pic max. (8192 chantillons): 0x0000138c #### 15% Pic max. (8192 chantillons): 0x000015ca #### 17%

et je voudrais juste récupérer la valeur avant le %

Merci

import re, subprocess
arec = subprocess.call(["arecord", "-D", "hw:0", "-c 2", "-d 0", "-f", "S16_LE", "-vvv", "/dev/null"])
max_amp = re.search("(\d*)%", arec)

À vérifier et adapter, je n’ai pas testé le code.
Tu as la doc du module re ici : docs.python.org/3.4/library/re.html

OK merci… Je garde ça dans un coin et je testerai plus tard (plus trop le temps là…)