【问题标题】:How do I get rid of the random apostrophes with my strings when justifying?证明时如何摆脱字符串中的随机撇号?
【发布时间】:2021-01-21 20:29:45
【问题描述】:

我正在做一个家庭作业,并且必须创建一个程序来创建一个文件目录,该目录也使用一个捕获异常块。我希望通过将字段全部对齐来使其看起来不错,因此它看起来像一个正确的输出表,但是我的所有字符串(创建的文件名和日期)周围都有这些随机撇号。但是,我的未对齐字符串和对齐的整数值没有。

我的代码:

import os, sys, time

try:
    path="."   
    dirs=os.listdir(path)
except IOError:
    print("Error: can't find file or read data")
else:
    print(repr("FILE").ljust(32), repr("SIZE").ljust(15), repr("DATE CREATED").ljust(30), end=" ")
    print("\n")
    for file in dirs:
        print(repr(file).ljust(30), repr(os.stat(file).st_size).rjust(8), end=' ')
        print("bytes", repr(time.ctime(os.path.getctime(file))).rjust(30))

My Output can be seen here.

【问题讨论】:

标签: python justify


【解决方案1】:

使用str(...) 而不是repr(...)

>>> repr(123)
'123'
>>> str(123)
'123'
>>> repr("string")
"'string'"
>>> str("string")
'string'

【讨论】:

  • 谢谢!我以前从未在 python 中证明过,所以这对我来说是一种奇怪的格式。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-04-02
  • 2012-01-26
  • 2018-09-06
  • 2012-11-19
  • 2018-10-08
  • 1970-01-01
  • 2021-10-01
相关资源
最近更新 更多