1

我有一个代码,它在某个阶段具有以下行:

n_brace.extend([numEL, valueLX[j],valueB[jj], el[2]])

此行包含在 for 循环中,因此它产生例如:

n_brace:
[367, '62', '141', '142', 369, '124', '156', '155', 379, '344', '266', '265', 381, '313', '251', '252']

但是我希望它改为:

n_brace:
[[367, '62', '141', '142'], [369, '124', '156', '155'], [379, '344', '266', '265'], [381, '313', '251', '252']]

在程序结束时,我打开一个文件并想要写入n_brace它:

fbrace = open("C:/Abaqus_JOBS/brace.txt", 'w')
fbrace.write(n_brace)

我希望结果是:

367, 62, 141, 142
369, 124, 156, 155
379, 344, 266, 265
381, 313, 251, 252

但我收到以下错误:

TypeError: expected a character buffer object

有任何想法吗?

4

1 回答 1

3

更改extend()append()

n_brace.append([numEL, valueLX[j],valueB[jj], el[2]])

至于写作(假设 Python 2.x):

with open("C:/Abaqus_JOBS/brace.txt", 'w') as fbrace:
    for row in n_brace:
        print >>fbrace, ', '.join(map(str, row))
于 2013-03-20T14:45:51.550 回答