python怎么读取txt文件内容然后保存到excel

1 /*

2 *language:c++3 *version:114 *encoding:GBK5 *made by Luo Wenshui6 *last modified:2020/5/107 *data file:input.txt8 */

9 #include

10 #include

11 #include

12 #include

13 #include

14 #include

15 using namespacestd;16 inline intread(){17 int ans=0,w=1;18 char ch=getchar();19 while(!isdigit(ch)){if(ch=='-')w=-1;ch=getchar();}20 while(isdigit(ch))ans=(ans<<3)+(ans<<1)+ch-'0',ch=getchar();21 return ans*w;22 }23 intn,m,t;24 const int maxn=1200;25 const int maxm=50;26 struct node{//每一件参赛作品

27 doublefinal_score;28 double raw_score[4];29 int score_givener[4];30 double std_score[4];31 int rank;//该作品在所有参赛作品中的排序

32 }a[maxn];33 structspecialist{34 double raw_score[100];35 double std_score[100];36 int num_of_work[100];//每次评阅的作品编号

37 int count;//表示专家批阅的项目数量;

38 double std;//该专家打分的标准差

39 double mean;//该专家打分的均值

40 int rank[maxn];//该专家对相应编号作品的打分在该专家所有分数中的排名

41 doubledelta0i,delta1i;42 double P0i,P1i;//定义为P0i=1-delta0i,P1i=1-delta1i

43 double P0T,P1T;//专家多次比赛获得的平均可行度水平,本论文中不涉及

44 int kide;//专家在聚类之后所属的类别

45 }E[maxm];46 struct work{//对所有作品确定排名的排序类

47 intindex;48 doublescore;49 }w[maxn];50 double R[maxm][maxm];//相似度矩阵

51 double total_mean;//所有专家打分的均值

52 double total_std;//所有专家打分的标准差

53 int kide[100]={3,2,3,3,3,2,3,3,3,1,3,2,2,1,3,3,3,2,3,2,1,3,2,3,2,1,2,3,2,1,3,2,3,3,3,3,2,1,3,3,3,2,2,3,1};//专家类别表,由聚类分析得出

54 double get_std(specialist& s)//获取每个专家打分的标准差

55 {56 double mean=0.0;57 for(int i = 1; i <= s.count; i ++ )//计算均值

58 {59 mean+=s.raw_score[i];60 }61 mean/=s.count;62 double ans = 0.0;63 for(int i = 1; i <= s.count; i ++)64 {65 ans+=(s.raw_score[i]-mean)*(s.raw_score[i]-mean);66 }67 return sqrt(ans/s.count);68 }69 double get_mean(specialist& s)//获取s打分的均值

70 {71 double ans = 0.0;72 for(int i = 1; i <= s.count; i ++)73 {74 ans +=s.raw_score[i];75 }76 return ans/s.count;77 }78 int get_rank(double score,specialist& s)//获取某一得分在专家所有打分中的排名

79 {80 int rank = 0;81 for(int i = 0; i <= s.count; i ++)82 {83 if(s.raw_score[i] < score)rank++;84 }85 returnrank;86 }87 double get_delta0i(specialist& Ei)//获取专家E[i]的delta0i

88 {89 double ans = 0.0;90 for(int j = 1; j <= Ei.count; j ++)91 {92 double final_score = a[Ei.num_of_work[j]].final_score;//专家Ei在评阅的作品j的总评得分

93 ans += fabs(Ei.raw_score[j]-final_score)/final_score;94 }95 return ans/(double)Ei.count;96 }97 double get_delta1i(specialist& Ei)//获取专家E[i]的delta1i

98 {99 double ans = 0.0;100 int count =Ei.count;101 for(int j = 1; j <= Ei.count; j ++)102 {103 double rank = a[Ei.num_of_work[j]].rank;//Ei评阅的第j件作品的总评排序

104 ans += fabs((double)Ei.rank[j]-rank)/(double)(count-1);105 }106 return ans/count;107 }108 void get_R()//计算定义的相似度矩阵

109 {110 double M = -1e10;111 for(int i = 1; i <= 45; i ++)112 {113 for(int j = 1; j <= 45; j ++)114 {115 double P0i =E[i].P0i;116 double P0j =E[j].P0i;117 double P1i =E[i].P1i;118 double P1j =E[j].P1i;119 double tmp = P0i*P0j+P1i*P1j;120 M =max(M,tmp);121 if(i == j)R[i][j]=1;122 else

123 {124 R[i][j]=tmp;125 }126 }127 }128 for(int i = 1; i <= 45; i ++)129 {130 for( int j = 1; j <= 45; j ++)131 {132 if(i==j)continue;133 R[i][j] = R[i][j]/M;//映射到(0,1)区间

134 }135 }136 }137 double get_total_mean()//获得总体均值

138 {139 double ans = 0.0;140 for(int i = 1; i <= 1046; i ++)141 {142 ans +=a[i].final_score;143 }144 return ans/1046;145 }146 double get_total_std()//获得总体方差

147 {148 double ans = 0.0;149 int count = 0;150 for(int i = 1; i <= 45;i ++)151 {152 count +=E[i].count;153 for(int j = 1; j <= E[i].count; j ++)154 {155 int num =E[i].num_of_work[j];156 ans += pow(a[num].final_score-E[i].raw_score[j],2);157 }158 }159 return sqrt(ans/count);160 }161 void display();//展示专家信息

162 void display_R();//展示相关系数矩阵

163 void display_P();//打印可信度表

164 structtest{165 intindex;166 };167

168 bool cmp(work& a1,work& a2){return a1.score

169 intmain()170 {171 freopen("input.txt","r",stdin);172 //freopen("output.txt","w",stdout);

173 std::ios::sync_with_stdio(false);174 intnumber,number_of_work;175 doubletmp;176 intx;177 doubletmp1,tmp2;178 for(int i = 1;i <= 1046;i ++)//获取1046条数据

179 {180 cin>>tmp;181 number = (int)tmp;182 cin>>a[number].final_score;183 w[i].index =i;184 w[i].score =a[number].final_score;185 //获取三个专家的信息

186 for(int j = 1; j >tmp;189 number = (int)tmp;190 cin>>tmp1>>tmp2;191

192 int count = ++E[number].count;193 E[number].raw_score[count]=tmp1;194 E[number].std_score[count]=tmp2;195 E[number].num_of_work[count]=i;//评阅的作品编号

196

197 a[i].raw_score[j] =tmp1;198 a[i].score_givener[j] =number;199 a[i].std_score[j] =tmp2;200 }201 }202

203 for(int i = 1; i <= 45; i ++)204 {205 E[i].std =get_std(E[i]);206 E[i].mean =get_mean(E[i]);207 for(int j = 1; j <= E[i].count; j ++ )//计算在E[i]所打的分中作品Aj的排名

208 {209 E[i].rank[j] =get_rank(E[i].raw_score[j],E[i]);210 }211 }212 sort(w+1,w+1046+1,cmp);//对作品final_score进行升序排序

213 for(int i = 1; i <=1046; i ++)214 {215 a[w[i].index].rank=i;//得到每件作品的排名

216 }217

218 for(int i = 1; i <= 45; i ++ )//获取每位专家的分值偏差和排序偏差与可信度

219 {220 E[i].delta0i =get_delta0i(E[i]);221 E[i].delta1i =get_delta1i(E[i]);222 E[i].P0i = 1 -E[i].delta0i;223 E[i].P1i = 1 -E[i].delta1i;224 }225 //cout<

230 //display_P();

231 }232 voiddisplay()233 {234 cout< 该专家第a次批阅了作品b,给了c分,该作品的标准分是d"<

248 cout<<"P0i ="<

python怎么读取txt文件内容然后保存到excel

CSDN 社区图书馆,开张营业!
python怎么读取txt文件内容然后保存到excel
深读计划,写书评领图书福利~