python读取txt数据处理后.csv格式输出
本代码读取了一个两列的txt文本文件,对其中数据进行处理画图后,并用csv格式输出:#!/usr/bin/pythonimport numpy as npimport matplotlib.pyplot as pltimport csv#plt.rc('font',family='Times New Roman')x=[]y=[]xcorr=[]ycorr=[]with...
·
本代码读取了一个两列的txt文本文件,对其中数据进行处理画图后,并用csv格式输出:
#!/usr/bin/python
import numpy as np
import matplotlib.pyplot as plt
import csv
#plt.rc('font',family='Times New Roman')
x=[]
y=[]
xcorr=[]
ycorr=[]
with open('Cd.out','r') as data:
for line in data:
p = line.split()
x.append(float(p[0]))
y.append(float(p[1]))
for i in range(len(x)):
x[i] = x[i]*96.1/(0.00005*0.33333)
y[i] = y[i]/0.33333/0.33333
if (x[i]>40):
xcorr.append(x[i])
ycorr.append(y[i])
with open("Cd_corr.csv","w") as outfile:
writer = csv.writer(outfile,delimiter = '\t')
writer.writerows(zip(xcorr,ycorr))
plt.figure(num=1, figsize=(8,6))
plt.rc('text',usetex=True)
plt.rc('font',family='serif')
plt.title('Time-averaged overall HTC', size=14)
plt.xlabel(r"tU/d",size = 14)
plt.ylabel('Cd', size=14)
plt.plot(xcorr, ycorr, 'r', label='Present study')
plt.legend(loc='center right')
plt.savefig('post_cd.png', format='png',dpi=300)
注意: csv用excel打开数据都在第一列,解决办法为CSV保存时设置了一定的分隔符号,在Excel中打开文件,选中第一列,点击菜单栏–数据–分列–分隔符号
.。原文参考自:http://www.aisun.org/2017/10/excel+csv/
更多推荐
已为社区贡献1条内容
所有评论(0)