POPMUISE
您可以使用 pythons csv-module来实现这一点。您需要指定quotechar="'"并quoting=csv.QUOTE_NONNUMERIC确保您想要的引用:import numpy as nplabels = 'L0', 'L1', 'L2'x = {'a': np.array([1.0, 1.1, 1.2]), 'b':np.array([2.0, 2.1, 2.2])}import csvwith open("d.txt","w", newline="") as f: # ensure quoting of non numerics wr = csv.writer(f, quotechar="'", quoting=csv.QUOTE_NONNUMERIC) header = ["Label"] + [k for k in x.keys()] wr.writerow(header) data = zip(labels,*x.values()) for d in data: wr.writerow(d)print(open("d.txt").read())文件输出:'Label','a','b''L0',1.0,2.0'L1',1.1,2.1'L2',1.2,2.2