Discuss / Python / mac读取windows产生的txt文件乱序

mac读取windows产生的txt文件乱序

Topic source

zhigang-ac

#1 Created at ... [Delete] [Delete and Lock User]

廖老师: 按照您给的代码我用python3写了一个读取txt文件读取的代码,系统是macos系统,但是读取出来的txt文件展示的内容和原来的文件不同,而在sublime中打开的就是对的,不知道为什么,请教老师! 代码: ef get_Rrs(infile,start_line,end_line): filename = os.path.basename(infile).split('.txt')[0].split('-') chla = filename[1];spim=filename[2];ag440 = filename[3] count = 1;waves = [];Rrs = [] with open(infile,'r') as f: for line in f.readlines():
print(line) if count>end_line: break if count>=start_line: data = line.split(' ')
print(data) waves.append(float(data[0].strip())) Rrs.append(float(data[1].strip()))
count = count+1 return [chla,spim,ag440,waves,Rrs]

读取出来的前几行:

" ECOLIGHT Run Title: Replace the rootname and title (03/16/2018 13:36)" "Wavelength = 402.5 nm" 100 4 "(num of wavelengths, num of sheets)" "line 4: reserved for future use" "line 5: reserved for future use" 402.5 1 "(wavelength and wavelength band)"

而sublime打开的是这样的: " ECOLIGHT Run Title: Replace the rootname and title (03/16/2018 13:36)" 100 20 "(num of wavelengths, num of sheets)" "wavelength (nm)" "depth (m)" "line 5: reserved for future use" "S0" "S0 W/(m^3 nm)" 100 32 " "

zhigang-ac

#2 Created at ... [Delete] [Delete and Lock User]

已经解决,是读取到文件名错误了。


  • 1

Reply