python - How to align and add space while writing dataframe tocsv in python
问题描述
Just wanted to add space either side to all cell content in DataFrame and needs to be Right aligned. [Input Format Each column with Tab Space][1] [1]: https://i.stack.imgur.com/MM7e0.png
[Output Format with Each column needs to be separated with 5 spaces and right aligned][2] [2]: https://i.stack.imgur.com/unc6B.png
I tried with Str.pad ( its applicable for Strings and prefix applicable only for Headers)
Thanks in advance
解决方案
查看numpy.linspace并在此处查看示例。您也可以通过执行以下操作手动执行此操作,例如:
with open(filename, 'r') as f:
in_lines = f.readlines()
with open(outfile, 'w') as f:
eight_spaces = ' '
for line in in_lines:
split_line = line.split()
out_line = eight_spaces.join(split_line)
f.write(out_line + '\n')
推荐阅读
- android - 以编程方式设置后获取android视图按钮的宽度
- java - Visual Studio Code Java JUnit 未输出到控制台
- c# - 我该如何解决这个错误“System.ServiceModel.FaultException”
- excel - 如何基于多个选项卡相关值进行嵌套搜索?
- r - Cut by day 省略了年初和年末的日子
- bash - 如何按特定顺序提取行
- apache-spark - Pickle 对象(模型数据)在 SPARK 流中遇到内存问题
- python - 如何在 Python 3 的 .txt 文件中找到单词的位置
- react-native - React Navigation:从drawerNavigator外部调度openDrawer时如何防止堆栈弹出?
- mongodb-query - 对 mongodb 集合进行分组并将结果作为单个对象输出