Python数据格式转换
代码
import pandas as pd
data = pd.read_excel("data.xls")
print(data, type(data))
# index=False 去掉行索引
# header=False 去掉列索引
# sep=' ' 元素之间为一个空格进行分割
data.to_csv("data_test.txt", index=False, header=False, sep=' ')
运行结果
被读取的本地文件(data.xls)
文件内容
写入本地的文件(data_test)
文件内容
C++读取转换后的数据
思路:
1.使用fscanf()将数据读取带结构体变量Ts中,再将存入到vector容器中
2.通过封装的PrintVector()可以看到打印是正确的
代码
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<fstream>
#include<vector>
#include<stdio.h>
using namespace std;
typedef struct Test
{
int rf;
int pa;
int pw;
}Ts;
void PrintVector(vector<Ts>&v)
{
for (vector<Ts>::iterator it = v.begin(); it != v.end(); it++)
{
cout << "rf = " << it->rf << endl;
cout << "pa = " << it->pa << endl;
cout << "pw = " << it->pw << endl;
}
return;
}
void test01()
{
FILE* fp = NULL;
vector<Ts>v;
Ts t = {0, 0, 0};
fp = fopen("data_test.txt", "r");
if (fp == NULL)
{
cout << "fp fopen fail!" << endl;
return;
}
while (fscanf(fp,"%d%d%d\n", &(t.rf), &(t.pa), &(t.pw)) != EOF)
{
v.push_back(t);
}
PrintVector(v);
fclose(fp);
fp = NULL;
return;
}
int main()
{
test01();
return 0;
}