已关闭。此问题需要details or clarity。当前不接受答案。
**想要改进此问题?**添加详细信息并通过editing this post阐明问题。
3小时前关门了。
Improve this question
我是Python新手。我正在尝试打开一个文本文件,并将其转换为数组
文本文件如下所示(textile.txt)
1,2022-05-01,37.2,70,18,120,80,95
1,2022-11-01,37.0,72,17,122,80,94
1,2023-05-01,37.1,71,16,121,78,93
1,2023-11-01,37.3,73,18,119,82,96
1,2024-05-01,37.0,69,19,120,80,95
2,2022-06-01,37.6,75,20,130,85,96
2,2022-12-01,37.8,74,19,128,84,95
2,2023-06-01,37.5,72,18,126,83,94
2,2023-12-01,37.7,73,21,130,86,97
2,2024-06-01,37.5,71,20,127,82,94
3,2022-07-01,36.9,68,16,118,75,93
3,2023-01-01,37.0,69,17,119,76,94
3,2023-07-01,36.8,67,15,117,74,92
3,2024-01-01,37.1,70,17,120,77,95
3,2024-07-01,36.9,68,16,118,75,93
我需要一个像这样的数组。
my_output = []
{1: [['2022-06-01', 37.2, 72, 16, 120, 80, 97], ['2022-09-15', 37.5, 73, 18, 125, 82, 96]], 2: [['2022-06-10', 36.9, 69, 18, 123, 80, 96], ['2023-01-03', 37.1, 71, 19, 124, 81, 97]], 3: [['2022-08-05', 37.3, 74, 20, 126, 83, 95], ['2023-03-01', 37.4, 75, 18, 123, 79, 98]]}
数据类型有int,data,float,int,int,int,int
这就是我一直在尝试的
my_output = []
with open ("textile.txt") as f:
for line in f:
id = [item.strip() for item in line.split(',')]
my_output = append(id)
print(my_output)
我得到的返回值是[]
3条答案
按热度按时间oknrviil1#
kknvjkwl2#
您可以使用
defaultdict
来存储与每个id对应的所有值。bvn4nwqk3#
使用“my_output.append(id)”代替“my_output = append(id)”,append是“list”的类函数,不能直接使用