作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我只想阅读此文本文件的第四和第五列
样本.txt
2012-01-01 09:00 San Jose Men's Clothing 214.05 Amex
2012-01-01 09:00 Fort Worth Women's Clothing 153.57 Visa
2012-01-01 09:00 San Diego Music 66.08 Cash
我可以做到这一点,但输出的格式不是我想要的。
输出
Men's Clothing 214.05
Women's Clothing 153.57
Music 66.08
所需输出
Men's Clothing 214.05
Women's Clothing 153.57
Music 66.08
这是我的代码(我使用了一些格式,但它仍然没有给我所需的输出):
for line in open("sample.txt"):
strsales=line.split()[-2]
item=line.split('\t')[3]
itemsales=item+"{0:>33}".format(strsales)
print(itemsales)
最佳答案
这一行:
itemsales=item+"{0:>33}".format(strsales)
正在将数字填充到33个位置。但是 item
的长度是可变的,这会导致您的对齐失败。仔细观察,你会发现第1行和第2行的凹凸有2个位置,这正是男装
和女装
的长度差异。您需要将两个变量填充到恒定数量的位置。试试这个
itemsales = "{0:<33}{1:>18}".format(item,strsales)
关于python - Python 中的对齐列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48365556/
我是一名优秀的程序员,十分优秀!