gpt4 book ai didi

python - 语法错误: unexpected EOF while parsing in ast.py while converting text into list of tuples

转载 作者:行者123 更新时间:2023-12-03 08:26:10 25 4
gpt4 key购买 nike

我有一个名为.txtmy_file文件,其中包含以下字符串行:

[(11.0795, 16), (12.0354, 16)]
[(11.0795, 19), (12.0354, 19)]
[(11.0795, 16), (12.0354, 16)]

现在,我正在使用以下代码将行转换为元组列表:
import ast


file_name = open('/home/username/Desktop/my_file.txt', "r")
lines = file_name.read().split('\n')
xy = ast.literal_eval(str(lines[0].split('"')).strip("[]").strip("'"))
xz = ast.literal_eval(str(lines[1].split('"')).strip("[]").strip("'"))
yz = ast.literal_eval(str(lines[2].split('"')).strip("[]").strip("'"))
print(xy[0])
print(yz[1])
print(xz[0][0])

但是,我收到以下错误:
File "/home/username/anaconda3/lib/python3.6/ast.py", line 48, in literal_eval
node_or_string = parse(node_or_string, mode='eval')
File "/home/username/anaconda3/lib/python3.6/ast.py", line 35, in parse
return compile(source, filename, mode, PyCF_ONLY_AST)
File "<unknown>", line 0

^
SyntaxError: unexpected EOF while parsing

我不确定为什么大多数情况下一切正常,但是对于此特定文件则不行。任何帮助表示赞赏!

最佳答案

这里的问题是,当您通过\n在换行符上分割时,您的输出中也将出现空行,并且当您尝试在它们上运行ast.literal_eval时,它们会引发异常。

让我们运行您的原始代码,这里我将内容存储在字符串而不是文件中。

s = """
[(11.0795, 16), (12.0354, 16)]
[(11.0795, 19), (12.0354, 19)]
[(11.0795, 16), (12.0354, 16)]
"""

import ast

lines = s.split('\n')
xy = ast.literal_eval(str(lines[0].split('"')).strip("[]").strip("'"))
xz = ast.literal_eval(str(lines[1].split('"')).strip("[]").strip("'"))
yz = ast.literal_eval(str(lines[2].split('"')).strip("[]").strip("'"))
print(xy[0])
print(yz[1])
print(xz[0][0])

现在,我确实得到了您遇到的错误。
Traceback (most recent call last):
File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydevd.py", line 1741, in <module>
main()
File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydevd.py", line 1735, in main
globals = debugger.run(setup['file'], None, None, is_module)
File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydevd.py", line 1135, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "/Applications/PyCharm CE.app/Contents/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "/Users/deveshks/PycharmProjects/DevProjects/script.py", line 11, in <module>
xy = ast.literal_eval(str(lines[0].split('"')).strip("[]").strip("'"))
File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ast.py", line 46, in literal_eval
node_or_string = parse(node_or_string, mode='eval')
File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ast.py", line 35, in parse
return compile(source, filename, mode, PyCF_ONLY_AST)
File "<unknown>", line 0

^
SyntaxError: unexpected EOF while parsing

现在,如果我修改拆分文本的行以忽略空行,则代码可以完美运行。
s = """
[(11.0795, 16), (12.0354, 16)]
[(11.0795, 19), (12.0354, 19)]
[(11.0795, 16), (12.0354, 16)]
"""

import ast


lines = [line.strip() for line in s.split('\n') if line.strip() != '']
xy = ast.literal_eval(str(lines[0].split('"')).strip("[]").strip("'"))
xz = ast.literal_eval(str(lines[1].split('"')).strip("[]").strip("'"))
yz = ast.literal_eval(str(lines[2].split('"')).strip("[]").strip("'"))
print(xy[0])
print(yz[1])
print(xz[0][0])

我现在得到输出
(11.0795, 16)
(12.0354, 16)
11.0795

关于python - 语法错误: unexpected EOF while parsing in ast.py while converting text into list of tuples,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55770575/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com