我正在用 Python 编写一个程序,它试图从同一目录中读取两个不同的文件。我的代码如下所示:
for i in spectra_files:
with open(i, 'r') as f:
numbers = [] #list to store all mass spectrum data directly from file before being processed
for line in f:
if line[0].isdigit(): #skips header in file
numbers.append(line)
str_data = ''.join(numbers)
#creates list for the mass and list for the intensity. to be used for plotting
spectrum_mass = [int(x.split(',')[0].strip()) for x in str_data.split()]
spectrum_intensity = [int(x.split(',')[1].strip()) for x in str_data.split()]
spectrum_mass.append('||')
spectrum_intensity.append('||')
spectra_files = spectra_files[spectra_files.index(i)+1:]
目前我让它遍历文件名称列表,然后将它们的数据添加到列表spectrum_mass和spectrum_intensity。但是,这不起作用,它只是添加列表中最后一个文件的数据。有什么建议么?