1.pandas--excel数据转换为txt格式

[复制链接]
发表于 2026-2-8 23:21:45 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
Python数据格式转换

代码

  1. import pandas as pd
  2. data = pd.read_excel("data.xls")
  3. print(data, type(data))
  4. # index=False  去掉行索引
  5. # header=False 去掉列索引
  6. # sep=' '      元素之间为一个空格进行分割
  7. data.to_csv("data_test.txt", index=False, header=False, sep=' ')
复制代码
运行效果


被读取的本地文件(data.xls)


文件内容


写入本地的文件(data_test)


文件内容


C++读取转换后的数据

思绪:
1.使用fscanf()将数据读取带布局体变量Ts中,再将存入到vector容器中
2.通过封装的PrintVector()可以看到打印是精确的
代码

  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include<iostream>
  3. #include<fstream>
  4. #include<vector>
  5. #include<stdio.h>
  6. using namespace std;
  7. typedef struct Test
  8. {       
  9.         int rf;
  10.         int pa;
  11.         int pw;
  12. }Ts;
  13. void PrintVector(vector<Ts>&v)
  14. {
  15.         for (vector<Ts>::iterator it = v.begin(); it != v.end(); it++)
  16.         {
  17.                 cout << "rf = " << it->rf << endl;
  18.                 cout << "pa = " << it->pa << endl;
  19.                 cout << "pw = " << it->pw << endl;
  20.         }
  21.         return;
  22. }
  23. void test01()
  24. {
  25.         FILE* fp = NULL;
  26.         vector<Ts>v;
  27.         Ts t = {0, 0, 0};
  28.         fp = fopen("data_test.txt", "r");
  29.         if (fp == NULL)
  30.         {
  31.                 cout << "fp fopen fail!" << endl;
  32.                 return;
  33.         }
  34.         while (fscanf(fp,"%d%d%d\n", &(t.rf), &(t.pa), &(t.pw)) != EOF)
  35.         {
  36.                 v.push_back(t);
  37.         }
  38.         PrintVector(v);
  39.         fclose(fp);
  40.         fp = NULL;
  41.         return;
  42. }
  43. int main()
  44. {
  45.         test01();
  46.        
  47.         return 0;
  48. }
复制代码
运行效果


回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表