使用python将数组从CSV文件显示为以下格式

我正在尝试将某些数据从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',)]


哆啦的时光机
浏览 245回答 3
3回答

一只萌萌小番薯

试试这个:# for the first outputoutput = tuple(int(x[0]) for x in X)# (1, 2, 3)# for the second outputoutput_2 = tuple((int(x[0]), ) for x in X)# ((1, ), (2, ), (3, ))

慕婉清6462132

这是一种方法:X = [('1',), ('2',), ('3',), ('4',), ('5',), ('6',)]from itertools import chainres1 = 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)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python