所以我试图从csv文件中获取数据并将其拆分为多个元组,具体取决于数据所在的行。我已经使用以下代码成功地将csv文件中的所有数据转换为一个元组:
import csv
def load_data(filename):
with open(filename) as f:
data=[tuple(line) for line in csv.reader(f)]
return data
运行此代码后 Output
:
[(' Slow Loris',' 21.72',' 29.3',' 20.08',' 29.98 ',' 29.85',' 26.22',' 19.99',' 27.78',' 27.02',' 22.44',' 26.54',' 26.46',' 22.24',' 27.72',' 22.25',' 28.52',' 25.57',' 22.44',' 27.06',' 24.24',' 21.74',' 26.72',' 27.23',' 21.48',' 26.14',' 21.09',' 28.28',' 29.05',' 23.52',' 24.74',' 29.08',' 21.57',' 21.98',' 26.66',' 21.99',' 20.91',' 28.07',' 26.06',' 22.54',' 25.9',' 21.78',' 25.96',' 25.25',' 28.08',' 23.79',' 29.99',' 21.41',' 21.67',' 22.94',' 29.68'),(' Ocelot',' 57.51',' 47.59',' 55.89',&#39 ; 47.15',' 46.71',' 51.7',' 46.68',' 48.36',&#39 ; 52.15',' 59.02',' 45.1',' 45.21',' 51.13',' 45.27',' 57.24',' 57.17',' 49.47',' 46.32',' 42.47',' 41.14',' 46.86',' 40.1',' 58.67',' 48.91',' 48.21',' 43.63',' 43.11',' 46.97',' 52.07',' 47.71',' 53.27',' 57.54',' 56.19',' 47.79',' 45.32',' 50.56',' 51.23',' 40.6',' 48.03',' 51.95',' 54.54'),(' Tiger',' 75.0',' 82.43',' 112.11',&#39 ; 89.93',' 103.19',' 80.6',' 113.44',' 105.44',&#39 ; 95.54',' 121.98',' 114.25',' 109.84',' 90.48',&#39 ; 105.84',' 82.89',' 113.64',' 102.73',' 104.57',' 100.83',' 75.59',' 79.86',' 91.11',' 94.75',' 109.89',' 117.39',' 74.71',' 71.04',' 92.97',' 88.87',' 92.95',' 86.67',' 101.46',' 92.4',' 85.2',' 107.19',' 117.81',' 90.95',' 82.02',' 87.31',' 106.53',' 86.28',' 106.62',' 107.57',' 89.38',' 105.88',' 74.45',' 90.03',' 107.96',' 77.42',' 98.9&#39 ;, ' 109.81',' 102.51',' 116.71',' 82.92',' 81.78',' 74.42',' 76.27',' 73.84',' 75.55',' 102.29',' 108.1',' 98.84',' 101.48',' 77.75',' 98.57',' 70.31',' 78.28',' 80.18&#39)]
我现在想知道的是我如何将其拆分为m ultiple tuples
,其中字符串表示应该形成新的元组?
答案 0 :(得分:2)
也许你想要的是:
import csv
def load_data(filename):
with open(filename) as f:
data = [(line[0], line[1:]) for line in csv.reader(f)]
return data
给出了:
[('Slow Loris', [' 21.72', ' 29.3', ' 20.08', ' 29.98', ' 29.85', ' 26.22', ' 19.99',
' 27.78', ' 27.02', ' 22.44', ' 26.54', ' 26.46', ' 22.24', ' 27.72',
' 22.25', ' 28.52', ' 25.57', ' 22.44', ' 27.06', ' 24.24', ' 21.74',
' 26.72', ' 27.23', ' 21.48', ' 26.14', ' 21.09', ' 28.28', ' 29.05',
' 23.52', ' 24.74', ' 29.08', ' 21.57', ' 21.98', ' 26.66', ' 21.99',
' 20.91', ' 28.07', ' 26.06', ' 22.54', ' 25.9', ' 21.78', ' 25.96',
' 25.25', ' 28.08', ' 23.79', ' 29.99', ' 21.41', ' 21.67', ' 22.94', ' 29.68']),
('Ocelot', [' 57.51', ' 47.59', ' 55.89', ' 47.15', ' 46.71', ' 51.7', ' 46.68',
' 48.36', ' 52.15', ' 59.02', ' 45.1', ' 45.21', ' 51.13', ' 45.27',
' 57.24', ' 57.17', ' 49.47', ' 46.32', ' 42.47', ' 41.14', ' 46.86',
' 40.1', ' 58.67', ' 48.91', ' 48.21', ' 43.63', ' 43.11', ' 46.97',
' 52.07', ' 47.71', ' 53.27', ' 57.54', ' 56.19', ' 47.79', ' 45.32',
' 50.56', ' 51.23', ' 40.6', ' 48.03', ' 51.95', ' 54.54']),
('Tiger', [' 75.0', ' 82.43', ' 112.11', ' 89.93', ' 103.19', ' 80.6', ' 113.44',
' 105.44', ' 95.54', ' 121.98', ' 114.25', ' 109.84', ' 90.48', ' 105.84',
' 82.89', ' 113.64', ' 102.73', ' 104.57', ' 100.83', ' 75.59', ' 79.86',
' 91.11', ' 94.75', ' 109.89', ' 117.39', ' 74.71', ' 71.04', ' 92.97',
' 88.87', ' 92.95', ' 86.67', ' 101.46', ' 92.4', ' 85.2', ' 107.19',
' 117.81', ' 90.95', ' 82.02', ' 87.31', ' 106.53', ' 86.28', ' 106.62',
' 107.57', ' 89.38', ' 105.88', ' 74.45', ' 90.03', ' 107.96', ' 77.42',
' 98.9', ' 109.81', ' 102.51', ' 116.71', ' 82.92', ' 81.78', ' 74.42',
' 76.27', ' 73.84', ' 75.55', ' 102.29', ' 108.1', ' 98.84', ' 101.48',
' 77.75', ' 98.57', ' 70.31', ' 78.28', ' 80.18'])]
答案 1 :(得分:2)
假设您的目标是将元组值与其标签字符串分开,您可以使用字典,将标签字符串存储为键。
鉴于ListView
是您提供的列表,此
l
会给你一个包含
的字典d = {t[0]: t[1:] for t in l}
然后,您可以通过迭代其键或值或两者来访问值元组
{'Tiger': (' 75.0', ' 82.43', ' 112.11', ' 89.93', ' 103.19', ' 80.6', ' 113.44', ' 105.44', ' 95.54', ' 121.98', ' 114.25', ' 109.84', ' 90.48', ' 105.84', ' 82.89', ' 113.64', ' 102.73', ' 104.57', ' 100.83', ' 75.59', ' 79.86', ' 91.11', ' 94.75', ' 109.89', ' 117.39', ' 74.71', ' 71.04', ' 92.97', ' 88.87', ' 92.95', ' 86.67', ' 101.46', ' 92.4', ' 85.2', ' 107.19', ' 117.81', ' 90.95', ' 82.02', ' 87.31', ' 106.53', ' 86.28', ' 106.62', ' 107.57', ' 89.38', ' 105.88', ' 74.45', ' 90.03', ' 107.96', ' 77.42', ' 98.9', ' 109.81', ' 102.51', ' 116.71', ' 82.92', ' 81.78', ' 74.42', ' 76.27', ' 73.84', ' 75.55', ' 102.29', ' 108.1', ' 98.84', ' 101.48', ' 77.75', ' 98.57', ' 70.31', ' 78.28', ' 80.18'),
'Slow Loris': (' 21.72', ' 29.3', ' 20.08', ' 29.98', ' 29.85', ' 26.22', ' 19.99', ' 27.78', ' 27.02', ' 22.44', ' 26.54', ' 26.46', ' 22.24', ' 27.72', ' 22.25', ' 28.52', ' 25.57', ' 22.44', ' 27.06', ' 24.24', ' 21.74', ' 26.72', ' 27.23', ' 21.48', ' 26.14', ' 21.09', ' 28.28', ' 29.05', ' 23.52', ' 24.74', ' 29.08', ' 21.57', ' 21.98', ' 26.66', ' 21.99', ' 20.91', ' 28.07', ' 26.06', ' 22.54', ' 25.9', ' 21.78', ' 25.96', ' 25.25', ' 28.08', ' 23.79', ' 29.99', ' 21.41', ' 21.67', ' 22.94', ' 29.68'), 'Ocelot': (' 57.51', ' 47.59', ' 55.89', ' 47.15', ' 46.71', ' 51.7', ' 46.68', ' 48.36', ' 52.15', ' 59.02', ' 45.1', ' 45.21', ' 51.13', ' 45.27', ' 57.24', ' 57.17', ' 49.47', ' 46.32', ' 42.47', ' 41.14', ' 46.86', ' 40.1', ' 58.67', ' 48.91', ' 48.21', ' 43.63', ' 43.11', ' 46.97', ' 52.07', ' 47.71', ' 53.27', ' 57.54', ' 56.19', ' 47.79', ' 45.32', ' 50.56', ' 51.23', ' 40.6', ' 48.03', ' 51.95', ' 54.54')}