我正在尝试将某些数据从CSV文件打印到python环境。编写此代码后,我得到了这种格式的输出。X = [('1',), ('2',), ('3',), ('4',), ('5',), ('6',)]以下两种格式的预期输出。每种都有不同的用法(1, 2, 3, 4, 5, 6)((1,) ,( 2 , ) , (3 , ) , (4 ,) , (5 ,), (6 ,) )但是我有兴趣像我提到的那样以其他某种格式显示输出。因为在ABAQUS软件工具中,它仅采用我提到的那些类型的格式。预先感谢您的时间和耐心。>filename='x.csv’with open(filename) as f:... data=[tuple(line) for line in csv.reader(f)]...>>> print data[('1',), ('2',), ('3',), ('4',), ('5',), ('6',)]
3 回答
一只萌萌小番薯
TA贡献1795条经验 获得超7个赞
试试这个:
# for the first output
output = tuple(int(x[0]) for x in X)
# (1, 2, 3)
# for the second output
output_2 = tuple((int(x[0]), ) for x in X)
# ((1, ), (2, ), (3, ))
慕婉清6462132
TA贡献1804条经验 获得超2个赞
这是一种方法:
X = [('1',), ('2',), ('3',), ('4',), ('5',), ('6',)]
from itertools import chain
res1 = tuple(map(int, chain.from_iterable(X)))
# (1, 2, 3, 4, 5, 6)
res2 = tuple((int(t[0]), ) for t in X)
# ((1,), (2,), (3,), (4,), (5,), (6,))
可替代地,以导出res2从res1和避免反复整数转换:
res2 = tuple((i,) for i in res1)
添加回答
举报
0/150
提交
取消
