有很多方法可以做到这一点,但从csv 库文档中的一些示例开始,这些示例展示了如何读取csv文件和写入新csv文件。import csvheader = []rows = []sortedrows = []with open('data.csv', 'r') as f: reader = csv.reader(f) # grab only the first row as the header/column labels header = next(reader, None) # read the csv data into list `rows` rows = list(reader)for row in rows: # convert all values to float for easy comparison nums = map(float, row) # sort values in descending order sortedrows.append(sorted(nums, reverse=True))# write sorted data to a new csv filewith open('sorted.csv', 'w') as f: writer = csv.writer(f) writer.writerow(header) writer.writerows(sortedrows)sorted.csv 应包含所需的结果:NumberA,NumberB22.0,10.044.0,33.0779.7,776.478.0,77.0在 Excel 中查看: