白人阅读 space
read by white space
我有一个包含以下单行的 .txt 文件
(1,65,b) (2,50,r) (3,80,b) (4,10,b) (5,60,b) (6,70,r) (8,5,r) (11,62,r)
元组的第一个元素是位置,第二个是值,第三个是颜色。例如对于第一个元组:position = 1, value = 65 and color is b.
如何以创建位置、值和颜色列表的方式读取此文件。
试试这个(代码注释中的解释):
file = "(1,65,b) (2,50,r) (3,80,b) (4,10,b) (5,60,b) (6,70,r) (8,5,r) (11,62,r)"
positions=[]
values=[]
colors=[]
# assuming that the values are stored in one line in the file, loop through text in file splitting text by space
for item in file.split(" "):
# split the text in between brackets [text betwen 1 to -1 >[1:-1] by comma
# append each item to corosponding list
positions.append(item[1:-1].split(",")[0])
values.append(item[1:-1].split(",")[1])
colors.append(item[1:-1].split(",")[2])
print(positions, values, colors)
一行解决方案:
file = "(1,65,b) (2,50,r) (3,80,b) (4,10,b) (5,60,b) (6,70,r) (8,5,r) (11,62,r)"
p,c,v = [list(map(lambda x:x[1:-1].split(",")[i], file.split(" ") )) for i in range(3)]
print (p,c,v)
您可以 regular expressions 搜索字符串。
import re
string = open ('text.txt').read()
position = list(map(int,re.findall(f'\(([0-9]+)',string)))
value = list(map(int,re.findall(f',([0-9]+)',string)))
color = list(re.findall(f'([a-z])\)',string))
print(position,value,color)
#output:
[1, 2, 3, 4, 5, 6, 8, 11] [65, 50, 80, 10, 60, 70, 5, 62] ['b', 'r', 'b', 'b', 'b', 'r', 'r', 'r']
这是另一个使用 replace 的简单解决方案。
import os
import time
import sys
output = "(1,65,b) (2,50,r) (3,80,b) (4,10,b) (5,60,b) (6,70,r) (8,5,r) (11,62,r)"
a = ((((output.replace("(",'')).replace(")",'')).replace(' ',',')).split(','))
e = a[0::3]
f = a[1::3]
g = a[2::3]
print(e,f,g)
Output:
['1', '2', '3', '4', '5', '6', '8', '11'] ['65', '50', '80', '10', '60', '70', '5', '62'] ['b', 'r', 'b', 'b', 'b', 'r', 'r', 'r']
我有一个包含以下单行的 .txt 文件
(1,65,b) (2,50,r) (3,80,b) (4,10,b) (5,60,b) (6,70,r) (8,5,r) (11,62,r)
元组的第一个元素是位置,第二个是值,第三个是颜色。例如对于第一个元组:position = 1, value = 65 and color is b.
如何以创建位置、值和颜色列表的方式读取此文件。
试试这个(代码注释中的解释):
file = "(1,65,b) (2,50,r) (3,80,b) (4,10,b) (5,60,b) (6,70,r) (8,5,r) (11,62,r)"
positions=[]
values=[]
colors=[]
# assuming that the values are stored in one line in the file, loop through text in file splitting text by space
for item in file.split(" "):
# split the text in between brackets [text betwen 1 to -1 >[1:-1] by comma
# append each item to corosponding list
positions.append(item[1:-1].split(",")[0])
values.append(item[1:-1].split(",")[1])
colors.append(item[1:-1].split(",")[2])
print(positions, values, colors)
一行解决方案:
file = "(1,65,b) (2,50,r) (3,80,b) (4,10,b) (5,60,b) (6,70,r) (8,5,r) (11,62,r)"
p,c,v = [list(map(lambda x:x[1:-1].split(",")[i], file.split(" ") )) for i in range(3)]
print (p,c,v)
您可以 regular expressions 搜索字符串。
import re
string = open ('text.txt').read()
position = list(map(int,re.findall(f'\(([0-9]+)',string)))
value = list(map(int,re.findall(f',([0-9]+)',string)))
color = list(re.findall(f'([a-z])\)',string))
print(position,value,color)
#output:
[1, 2, 3, 4, 5, 6, 8, 11] [65, 50, 80, 10, 60, 70, 5, 62] ['b', 'r', 'b', 'b', 'b', 'r', 'r', 'r']
这是另一个使用 replace 的简单解决方案。
import os
import time
import sys
output = "(1,65,b) (2,50,r) (3,80,b) (4,10,b) (5,60,b) (6,70,r) (8,5,r) (11,62,r)"
a = ((((output.replace("(",'')).replace(")",'')).replace(' ',',')).split(','))
e = a[0::3]
f = a[1::3]
g = a[2::3]
print(e,f,g)
Output:
['1', '2', '3', '4', '5', '6', '8', '11'] ['65', '50', '80', '10', '60', '70', '5', '62'] ['b', 'r', 'b', 'b', 'b', 'r', 'r', 'r']