Python “如何替换”;“未定义”;字符串中包含0?

Python “如何替换”;“未定义”;字符串中包含0?,python,string,replace,undefined,Python,String,Replace,Undefined,我目前正在用Python编写一个程序,它读取Praat生成的语音报告,提取每个变量的值并将其写入csv文件。我已经检查了Python中包含语音报告的变量的类型,它似乎是String类型,正如预期的那样。但是,当我尝试使用以下方法将--undefined--的实例替换为“0”时,它似乎不起作用 voice_report_str.replace('undefined', '0') 有人能告诉我为什么这种方法不能像预期的那样有效吗 Praat生成的语音报告: 音高: 中距:124.541赫兹 平均螺

我目前正在用Python编写一个程序,它读取Praat生成的语音报告,提取每个变量的值并将其写入csv文件。我已经检查了Python中包含语音报告的变量的类型,它似乎是String类型,正如预期的那样。但是,当我尝试使用以下方法将--undefined--的实例替换为“0”时,它似乎不起作用

voice_report_str.replace('undefined', '0')
有人能告诉我为什么这种方法不能像预期的那样有效吗

Praat生成的语音报告:

音高:
中距:124.541赫兹
平均螺距:124.518赫兹
标准偏差:5.444赫兹
最小螺距:119.826赫兹
最大螺距:132.017 Hz
脉冲:
脉冲数:4
期间数:3
平均周期:8.269978E-3秒
周期标准偏差:0.372608E-3秒
发言:
局部清音框架的比例:94.000%(94/100)
断音次数:0
声音中断程度:0(0秒/0秒)
抖动:
抖动(本地):4.210%
抖动(本地、绝对):348.203E-6秒
抖动(rap):1.852%
抖动(ppq5):--未定义--
抖动(ddp):5.556%
微光:
微光(局部):--未定义--
微光(本地,dB):--未定义--dB
微光(apq3):--未定义--
微光(apq5):--未定义--
微光(apq11):--未定义--
微光(dda):--未定义--
仅浊音部分的和谐度:
平均自相关:0.670552
平均噪声谐波比:0.514756
平均谐波噪声比:3.176 dB
我的代码

def measurePitch(voiceID, f0min, f0max, unit, startTime, endTime):
    sound = parselmouth.Sound(voiceID) # read the sound
    pitch = call(sound, "To Pitch", 0.0, f0min, f0max) #create a praat pitch object
    pulses = parselmouth.praat.call([sound, pitch], "To PointProcess (cc)")
    duration = parselmouth.praat.call(pitch, "Get end time")
    voice_report_str = parselmouth.praat.call([sound, pitch, pulses], "Voice report", startTime, endTime, 75, 600, 1.3, 1.6, 0.03, 0.45)
    voice_report_str.replace('undefined', '0')
    s=re.findall(r'-?\d+\.?\d*',voice_report_str)
    print(voice_report_str)
    print(len(s))
    report = [s[21], s[22]+'E'+s[23],s[24],s[26],s[27],s[28],s[29],s[31],s[33],s[35]]

    return report

我看不到您的全部代码,但我假设您做了如下操作:

text = "This is my text"
text.replace("my", "0") # nothing really happened, text is still the same
# text == "This is my text"
所以你应该这样做:

text = "This is my text"
text = text.replace("my", "0") # text is changed, yay
# text == "This is 0 text"

它是用Python编写的-对不起,我应该说
replace
不会更改原始字符串。它返回一个修改过的副本。非常感谢!代码正在运行-感谢您的帮助