Python json文件只能采用整数或切片格式,不能采用字符串格式
我在运行下面的代码时遇到了一个问题,在tone\u freq=tone\u map[tone\u name行中,列表索引必须是整数或片而不是字符串,这会带来错误 注释音网站上的Json示例如下Python json文件只能采用整数或切片格式,不能采用字符串格式,python,Python,我在运行下面的代码时遇到了一个问题,在tone\u freq=tone\u map[tone\u name行中,列表索引必须是整数或片而不是字符串,这会带来错误 注释音网站上的Json示例如下 [ { "432": "Note", "434": "Frequency (Hz)", "436": "Wavelength (cm)", "438": "", "440": "", "442": "", "444": "", "446": "" },
[
{
"432": "Note",
"434": "Frequency (Hz)",
"436": "Wavelength (cm)",
"438": "",
"440": "",
"442": "",
"444": "",
"446": ""
},
{
"432": "C0",
"434": "16.35",
"436": "2109.89",
"438": "",
"440": "",
"442": "",
"444": "",
"446": ""
},
{
"432": "C#0/Db0",
"434": "17.32",
"436": "1991.47",
"438": "",
"440": "",
"442": "",
"444": "",
"446": ""
},
我用来合成音调以生成音乐的python代码是这样的
import json
import numpy as np
import matplotlib.pyplot as plt
from scipy.io.wavfile import write
# Synthesize the tone based on the input parameters
def tone_synthesizer(freq, duration, amplitude=1.0, sampling_freq=44100):
# Construct the time axis
time_axis = np.linspace(0, duration, duration * sampling_freq)
# Construct the audio signal
signal = amplitude * np.sin(2 * np.pi * freq * time_axis)
return signal.astype(np.int16)
if __name__=='__main__':
# Names of output files
file_tone_single = 'generated_tone_single.wav'
file_tone_sequence = 'generated_tone_sequence.wav'
# Source: http://www.phy.mtu.edu/~suits/notefreqs.html
mapping_file = 'tone_mapping.json'
# Load the tone to frequency map from the mapping file
with open(mapping_file, 'r') as f:
tone_map = json.loads(f.read())
# Set input parameters to generate 'F' tone
tone_name = 'F'
duration = 3 # seconds
amplitude = 12000
sampling_freq = 44100 # Hz
# Extract the tone frequency
tone_freq = tone_map[tone_name]
# Generate the tone using the above parameters
synthesized_tone = tone_synthesizer(tone_freq, duration, amplitude, sampling_freq)
# Write the audio signal to the output file
write(file_tone_single, sampling_freq, synthesized_tone)
# Define the tone sequence along with corresponding durations in seconds
tone_sequence = [('G', 0.4), ('D', 0.5), ('F', 0.3), ('C', 0.6), ('A', 0.4)]
# Construct the audio signal based on the above sequence
signal = np.array([])
for item in tone_sequence:
# Get the name of the tone
tone_name = item[0]
# Extract the corresponding frequency of the tone
freq = tone_map[tone_name]
# Extract the duration
duration = item[1]
# Synthesize the tone
synthesized_tone = tone_synthesizer(freq, duration, amplitude, sampling_freq)
# Append the output signal
signal = np.append(signal, synthesized_tone, axis=0)
# Save the audio in the output file
write(file_tone_sequence, sampling_freq, signal)
从python文档中: loads(s[,encoding[,cls[,object_hook[,parse_float[,parse_int[,parse_constant[,object_pairs_hook[,**kw \\\\\\\\\\\\\\\]] 使用此转换表将s(包含JSON文档的str或unicode实例)反序列化为Python对象 如果s是str实例,并且使用基于ASCII的编码而不是UTF-8(例如拉丁语-1)进行编码,则必须指定适当的编码名称。不允许使用基于ASCII的编码(例如UCS-2),并且应首先将其解码为unicode 从错误中可以理解,
tone\u map
是一个列表。
试试这个:
对于色调映射中的i:
音调频率=i[音调名称]
将tone_freq存储在列表中并使用它
音调频率=列表()
对于色调映射中的i:
音调频率。附加(i)
无论您使用什么格式将HTML表()转换为JSON,似乎都是以一种非常不方便的格式完成的。如果您查看JSON文档,它是一个数组(而不是一个map/dict)。数组元素似乎与原始表行相对应,每个数组元素本身是一个dict,包含键“432”、“434”、“436”、“438”等。前三个是表的三列(名称、频率、波长),其余为空
您应该首先尝试将其重新格式化为更有用的格式,例如,注释名称和频率之间的映射(不要忘记将频率数据转换为浮点数-JSON文档将其作为字符串)。也许类似的操作可以:
name_column="432"
freq_column="434"
fmap = { row[name_column] : float(row[freq_column]) for row in json_doc[1:] }
(注意我已经切掉了第一个数组元素,它包含列名)
现在,您可以尝试:
freq = fmap["E0"]
print (freq)
另外,请注意,在示例Python代码中,您使用的注释名称如“C”,这在表中找不到(它有“C0”、“C1”等)。行号中有错误...“Json示例”是您的“tone_mapping.Json”文件吗?如果是,它不是映射(
dict
)但这是一个dict列表。如果我错了,请纠正我,但我认为您无法从列表中获取元素,只要它的索引为字符串。这不是字典