[英]create a second row for a list in python and write list to txt file
I have a code which at a stage has the following line: 我有一个代码,在一个阶段有以下行:
n_brace.extend([numEL, valueLX[j],valueB[jj], el[2]])
This line is included in a for loop so it produces for example: 这一行包含在for循环中,因此它产生例如:
n_brace:
[367, '62', '141', '142', 369, '124', '156', '155', 379, '344', '266', '265', 381, '313', '251', '252']
However I want it to be instead: 但是我希望它是:
n_brace:
[[367, '62', '141', '142'], [369, '124', '156', '155'], [379, '344', '266', '265'], [381, '313', '251', '252']]
At the end of the program I open a file and want to write n_brace
to it: 在程序结束时,我打开一个文件,并想写入n_brace
:
fbrace = open("C:/Abaqus_JOBS/brace.txt", 'w')
fbrace.write(n_brace)
I'd like the result to be: 我希望结果如下:
367, 62, 141, 142
369, 124, 156, 155
379, 344, 266, 265
381, 313, 251, 252
But I get the following error: 但是我收到以下错误:
TypeError: expected a character buffer object
Any ideas? 有任何想法吗?
Change extend()
to append()
: extend()
更改为append()
:
n_brace.append([numEL, valueLX[j],valueB[jj], el[2]])
As for the writing (assuming Python 2.x): 至于写作(假设Python 2.x):
with open("C:/Abaqus_JOBS/brace.txt", 'w') as fbrace:
for row in n_brace:
print >>fbrace, ', '.join(map(str, row))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.