The file name is studs.txt and this is what is inside:
12345 2 G400 Bart Simpson
12346 1 GH46 Lisa Simpson
12347 2 G401 Homer J Simpson
12348 4 H610 Hermione Grainger
12349 3 G400 Harry Potter
12350 1 G402 Herschel Shmoikel Krustofski
13123 3 G612 Wayne Rooney
x = input("Enter a filename: ")
try:
o = open(x, "r")
p = o.read()
y = tuple(j for j in p.splitlines())
m = " ".join(map(str, y))
print(m)
o.close()
except IOError as e :
print("File Does Not Exist")
当我运行此代码时,它将导入此文件“螺柱.txt“并将其放入字符串中,但我希望将该字符串转换为表格格式并以特定布局显示,以便使其看起来像这样:
Simpson, Bart 12345 G400 2
Simpson, Lisa 12346 G401 1
等等
我想你可以这样做:
这张照片:
相关问题 更多 >
编程相关推荐