【问题标题】:Printing a Python Program for coordinates为坐标打印 Python 程序
【发布时间】:2018-10-05 17:48:38
【问题描述】:

我正在开发一个 python 程序,该程序需要我采取一系列要点(如下所列)。我的程序需要能够读取文本文件并将坐标打印到屏幕上,如下所示:

这是我一直使用的格式

点列表:

p1:4,13
p2:13,11
p3:0,8
p4:3,0

到目前为止我的程序

>>> coordinates = open('students.txt', 'r') 
>>> lines = coordinates.readlines() 
>>> lines 
['place,coordiantes\n', 'p1,4,13\n', 'p2,13,11\n', 'p3,0,8    \n','p4,3,0']
>>> for line in lines: 
...     print line.strip().split(',') 
['place', 'coordinate'] ['p1', '4,13'] ['p2', '13,11'] ['p3', '0,8']['p4','3,0']

我的问题是我需要删除位置之间的,,并在最终打印上进行坐标。有什么建议可以帮助解决这个问题吗?

【问题讨论】:

  • 但是如果它们是同一个子列表的两个元素,它们之间会用逗号打印。您可以使用for line in lines: for l in line.strip().split(','): print (l)。我不确定您到底希望它们如何打印
  • @Veridian 在运行您的代码时,我会按照您的需要正确拆分所有内容。您确定向我们展示了正确的代码和输出吗?

标签: python string xcode printing


【解决方案1】:
lines = ['place,coordiantes\n', 'p1,4,13\n', 'p2,13,11\n', 'p3,0,8    \n','p4,3,0']


lines = [stuff.replace("\n", "") for stuff in lines]
lines = [stuff.replace(" ", "") for stuff in lines]


points = {}

new_list = []
for line in lines:
    new_list.append(line.split(","))

for line in new_list:

    if len(line) > 2:
        print(line[0] + ": " + line[1] + "," + line[2])

给予:

p1: 4,13
p2: 13,11
p3: 0,8
p4: 3,0

这是你想要的吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-05
    • 2015-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-23
    • 2018-02-04
    相关资源
    最近更新 更多