Python将多行数组放在一行中

Python将多行数组放在一行中,python,Python,我有一个包含如下数据的文本文件: array([[a, b, c, d, e, f, g, h, i, j], [k, l, m, n, o, p, q, r, s, t], [u, v, w, x, y, z, 0, 1, 2, 3]]) 对于python,我需要将每个括号中的文本放在单行中,如: a b c d e f g h i j k l m n o p q r s t u v w

我有一个包含如下数据的文本文件:

array([[a, b, c, d, e,  
       f, g, h, i, j],  
       [k, l, m, n, o,     
       p, q, r, s, t],   
       [u, v, w, x, y,   
       z, 0, 1, 2, 3]])
对于python,我需要将每个括号中的文本放在单行中,如:

a b c d e f g h i j 
k l m n o p q r s t  
u v w x y z 0 1 2 3 

有什么建议吗?

我认为您在描述这个问题时做得不够好,现在还不清楚为什么会有这样的文件(我想大多数人都认为您在python中有一个现有的数组),其中包含数组定义,就好像它是为已经设置了变量的特定语言编写的一样,而不是带有qouted字符串等的数据馈送

尽管如此,只要您只有这些简单的值,就可以使用RegExp和JSON在几个步骤内完成这项工作。下面的脚本(请参阅的在线演示)逐步向您展示了如何清理数据并使其成为JSON字符串,然后可以使用python的JSON模块加载该字符串

import re
import json

# GC is done by Python, no need for file.close()
string = open('input.txt').read() 
# Remove the array declaration start
string = re.sub(r"^array\(", '', string)
# Remove the array end declaration
string = re.sub(r"\)$", '', string)
# Remove all whitespaces and newlines
string = re.sub(r"\s*|\n*", '', string)
# Quote all strings and numbers
string = re.sub(r"([a-zA-Z0-9]+)", r'"\1"', string)
# Should be good enough now to be read with json.loads
mainlist = json.loads(string)

print(
  "\n".join([" ".join(sublist) for sublist in mainlist])
)

'\n.join([''.join([i代表i in j])代表j in x])
谢谢Chrisz,请问完整的代码是什么?这是完整的代码x是什么?请与我们分享您尝试过的解决方案及其成果。