Python: error parsing strings from text file with Ast module

ぐ巨炮叔叔 提交于 2019-12-02 21:28:10

问题


I have a text file (.txt) with the following two strings as so (this is a snippet of a larger file where all strings have the same format):

["('a', '1')", "('b', '2')"]
["('c', '3')", "('d', '4')"]

I am using the below code to parse the strings in the text file so that I can access each element for data analysis. The code works for the string if I declare it as a variable, such as:

string = ["('c', '3')", "('d', '4')"]

But the code does not work for the text file:

import ast

text_file = open('text_file.txt', 'r')

for string in text_file:
    parsed = list([ast.literal_eval(x) for x in text_file])
    print parsed

I would expect to get:

[('a', '1'), ('b', '2')]
[('c', '3'), ('d', '4')]

But I get the following error:

Traceback (most recent call last):
  File "test.py", line 8, in <module>
    parsed = list([ast.literal_eval(x) for x in string])
  File "/usr/local/Cellar/python/2.7.8_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ast.py", line 49, in literal_eval
    node_or_string = parse(node_or_string, mode='eval')
  File "/usr/local/Cellar/python/2.7.8_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ast.py", line 37, in parse
    return compile(source, filename, mode, PyCF_ONLY_AST)
  File "<unknown>", line 1
    [
    ^
SyntaxError: unexpected EOF while parsing

回答1:


You need to apply ast.literal_eval function on the string itself. You don't need to iterate over the contents of the string.

with open('file') as f:
    for string in f:
        parsed = ast.literal_eval(string.strip())
        print parsed

Output:

["('a', '1')", "('b', '2')"]
["('c', '3')", "('d', '4')"]

OR

import re
with open('file') as f:
    for string in f:
        print re.findall(r"\('([^']*)',\s*'([^']*)'\)", string)

Output:

[('a', '1'), ('b', '2')]
[('c', '3'), ('d', '4')]



回答2:


import ast 

text_file = open('text_file.txt', 'r')

for string in text_file:
    print ast.literal_eval(string)


来源:https://stackoverflow.com/questions/29270031/python-error-parsing-strings-from-text-file-with-ast-module

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!