嗨,我在读取文件并尝试拆分包含2个列表的字符串时遇到麻烦。
我在文件文本中有此字符串:
BaseDeDatos.txt:
return
代码:
public static void main(String[] args) throws IOException
{
char[] array = new char[]{'a','b','c'};
System.out.println(arrayToString(array));
}
根据另一个问题的建议:question我尝试使用hello test, C, ['Gore', 'Family'], 3.4, Actor, ['Cesar', 'Mile'], 1
,但出现此错误:
with open('BaseDeDatos.txt', 'r') as data:
peli = data.readlines()[int(modificar_peli)-1]
selec_final = [ast.literal_eval(i) if i.startswith('[') else int(i) if re.findall('^\\d+$', i) else i for i in re.split(',\s*', peli)]
预期输出:
ast.literal_eval()
如果有帮助,当我只有1个列表时,我尝试使用相同的代码,并将其完美地分开。
我们将不胜感激:)
答案 0 :(得分:0)
s = "hello test, C, ['Gore', 'Family'], 3.4, Actor, ['Cesar', 'Mile'], 1"
splitted = re.split(r', (?!\')', s)
只有在狭缝表达式后没有单引号,
的情况下,才会在'
(逗号和空格)上分割行。有关前瞻语法的更多信息:https://docs.python.org/3/library/re.html#index-21
输出:
['hello test',
'C',
"['Gore', 'Family']",
'3.4',
'Actor',
"['Cesar', 'Mile']",
'1']
答案 1 :(得分:-1)
我认为这将帮助您解决问题。
Basetext = open("LAB3.txt", "r")
listfortext = []
for line in Basetext:
listfortext.append(line.split())
Basetext.close()