如何在列表中将它们分开?

huangapple go评论59阅读模式
英文:

How can i seperate these in a list?

问题

下面是您要翻译的内容,不包括代码部分:

我有一个名为“1011.txt”的文本文件,其中写有以下内容:
1011 7:30
1011 14:25
1011 8:00
1011 18:20
1011 7:45
1011 17:21

我想将它导入到我的Python文件中,所以我使用read和splitlines将其转化为一个列表(我不能做其他任何事情,我必须将其转化为一个列表)。

所以我现在必须拆分“代码”部分,即“1011”,以及1011后面的数字工作小时。我必须使用循环将它们分开(我还必须分开小时和分钟)。有人知道我该如何做吗?
我想将它们转化为列表中的单独项。

到目前为止,这是我的代码:

r1 = open("1011.txt")

l1 = r1.read().splitlines()
print(l1)
for hours in range(len(l1)):
    print(l1[hours])
r1.close()
英文:

so i have a text file named "1011.txt" with these things written into it:
1011 7:30
1011 14:25
1011 8:00
1011 18:20
1011 7:45
1011 17:21

i want to bring it into my python file, so i use read and splitlines to make it into a list.(i cant do anything else, i HAVE to make it into a list)

so i now have to split the "code" which is "1011" and the work hours which are the numbers after 1011.i have to seperate them with loops(i have to also separate the hour and the minute.)
does anybody know how i can do this?
i want to turn them into separate items in a list

so far this my code:

r1 = open("1011.txt")

l1 = r1.read().splitlines()
print(l1)
for hours in range(len(l1)):
    print ((l1[hours]))
r1.close()

答案1

得分: 1

str.split可以在这里使用:

for line in l1:
    code, time = line.split()
    hour, minute = time.split(':')
    print(code, hour, minute)
英文:

str.split can be used here:

for line in l1:
    code, time = line.split()
    hour, minute = time.split(':')
    print(code, hour, minute)

huangapple
  • 本文由 发表于 2023年2月24日 15:38:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75553751.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定