我一直在尝试从一个熊猫数据帧输出一个文件,它看起来像这样,我一直在尝试用不同的格式将它写入一个文件
codi Acumulado Ahorro Prestamo Fondo 1 Prestamo Empresa 1
0 0151 1425.76 28320 0.00
1 2361 1158.49 4100 1200.00
2 2663 737.10 2903 429.00
3 2792 897.75 1950 627.00
4 0737 1266.54 7859 0.00
5 5073 779.00 557 754.00
6 2819 1274.70 958 0.00
7 1558 738.51 10242 676.00
8 4105 695.49 2382 0.00
9 4146 1170.08 8623 0.00
10 0528 1337.17 1042 0.00
我试过这样的方法:
df.to_csv(output_file, sep = '|')
但是输出就像一个csv,而不是“,”我得到“|”
我正在努力实现这个输出:
0E0151
A1425.76|2019|Acumulado Ahorro
A0.00|2019|Prestamo Empresa 1
A28320.00|2019|Prestamo Fondo 1
其中0E必须连接到codi
列中的数字,然后将字母“A”连接到Acumulado ahorro
列中的数字,然后连接字符“|”和列的名称,依此类推
如何创建此格式以便将其写入实际文件
对于这种不寻常的文件格式,您需要一个自定义的写入例程,例如:
我非常肯定,你必须创建自己的自定义格式化程序。希望这个答案能提供一些指导
输出:
您可以轻松地将
format_text
保存到所需的任何文本文件中相关问题 更多 >
编程相关推荐