eval to dict问题NEWB疯了! [英] eval to dict problems NEWB going crazy !
问题描述
我有一个名为a.txt的文本文件:
#comments
[ (''recId'',3),(''解析'','''''''''''''''''''''''''''''''''''}'}}}
[(''recId'',5),('''解析'',{''pos'':u''np'',''gen'':你是''}}) ]
[(''recId'',7),('''解析'',{''pos'':u''np'',''gen'':你''' m''})]
我用它读了它:
filAnsMorph = codecs.open(''a.txt'', ''r'',''utf-8'')#Initialise输入
file
dicAnsMorph = {}
for filAnsMorph :
如果行[0]!=''#'':#摆脱评论行
x = eval(行)
dicAnsMorph [x [0] [1]] = x [1] [1] #recid是关键,解析词典是
值
但它崩溃了时间在x = eval(线)。为什么是这样?如果我将
a.txt更改为:
#comments
[(''recId'',3),( ''解析'',{''pos'':''''''''''''''''''''}')
它工作正常。为什么它不适用于多行?它让我开车了
疯了!
谢谢,
马修
Hi,
I have a text file called a.txt:
# comments
[(''recId'', 3), (''parse'', {''pos'': u''np'', ''gen'': u''m''})]
[(''recId'', 5), (''parse'', {''pos'': u''np'', ''gen'': u''m''})]
[(''recId'', 7 ), (''parse'', {''pos'': u''np'', ''gen'': u''m''})]
I read it using this:
filAnsMorph = codecs.open(''a.txt'', ''r'', ''utf-8'') # Initialise input
file
dicAnsMorph = {}
for line in filAnsMorph:
if line[0] != ''#'': # Get rid of comment lines
x = eval(line)
dicAnsMorph[x[0][1]] = x[1][1] # recid is key, parse dict is
value
But it crashes every time on x = eval(line). Why is this? If I change
a.txt to:
# comments
[(''recId'', 3), (''parse'', {''pos'': u''np'', ''gen'': u''m''})]
it works fine. Why doesn''t it work with multiple lines? it''s driving me
crazy!
Thanks,
Matthew
推荐答案
manstey写道:
manstey wrote:
我有一个文本文件名为a.txt:
#comments
[(''recId'',3),(''parse'',{'' pos'':u''np'',''gen'':你是''}})
[(''recId'',5),('''解析' ',{''pos'':你'''''''''''''''}})
[(''recId'',7), (''解析'',{''pos'':''''''''''''''''''''}'}}
我读过使用它:
filAnsMorph = codecs.open(''a.txt'',''r'',''utf-8'')#Initialise input
file
dicAnsMorph = {}
for filAnsMorph:
如果行[0]!=''#' ':#摆脱评论专栏
x = eval(line)
dicAnsMorph [x [0] [1]] = x [1] [1] #recid是关键,解析dict是
value
但它每次都在x = eval(行)时崩溃。为什么是这样?如果我将
a.txt更改为:
#comments
[(''recId'',3),( ''解析'',{''pos'':''''''''''''''''''''}')
它工作正常。为什么它不适用于多行?它让我开车了
疯了!
Hi,
I have a text file called a.txt:
# comments
[(''recId'', 3), (''parse'', {''pos'': u''np'', ''gen'': u''m''})]
[(''recId'', 5), (''parse'', {''pos'': u''np'', ''gen'': u''m''})]
[(''recId'', 7 ), (''parse'', {''pos'': u''np'', ''gen'': u''m''})]
I read it using this:
filAnsMorph = codecs.open(''a.txt'', ''r'', ''utf-8'') # Initialise input
file
dicAnsMorph = {}
for line in filAnsMorph:
if line[0] != ''#'': # Get rid of comment lines
x = eval(line)
dicAnsMorph[x[0][1]] = x[1][1] # recid is key, parse dict is
value
But it crashes every time on x = eval(line). Why is this? If I change
a.txt to:
# comments
[(''recId'', 3), (''parse'', {''pos'': u''np'', ''gen'': u''m''})]
it works fine. Why doesn''t it work with multiple lines? it''s driving me
crazy!
尝试:
x = eval(line.strip(''\ n''))
-
bruno desthuilliers
python -c" print''@''。join([''。''。join([w [:: - - 1] for p in p.split(''。'')])for
p in''o **** @ xiludom.gro''。split(''''')] )
try with:
x = eval(line.strip(''\n''))
--
bruno desthuilliers
python -c "print ''@''.join([''.''.join([w[::-1] for w in p.split(''.'')]) for
p in ''o****@xiludom.gro''.split(''@'')])"
那是行不通的。我刚收到一个错误:
x = eval(line.strip(''\ n''))
文件"< string>" ;,第1行
[(''recId'',3),(''解析'',{''pos'':你'''''''gen'':你好'}})
语法错误:解析时意外的EOF
任何其他想法?
Bruno Desthuilliers写道:
That doesn''t work. I just get an error:
x = eval(line.strip(''\n''))
File "<string>", line 1
[(''recId'', 3), (''parse'', {''pos'': u''np'', ''gen'': u''m''})]
SyntaxError: unexpected EOF while parsing
any other ideas?
Bruno Desthuilliers wrote:
manstey写道:
manstey wrote:
我有一个名为a.txt的文本文件:
#comments
[(''recId'',3),(''解析'',{''pos'':u''np'',''gen'':你是''}})
[(''recId'',5 ),(''解析'','''''''''''''''''''''''''''''''''''''' rec'd'',7),(''解析'',{''pos'':'''''''''''''''''''''}})
>
我读过我使用这个:
filAnsMorph = codecs.open(''a.txt'',''r'',''utf-8'')#Initialise input
file
dicAnsMorph = {}
for filAnsMorph:
如果行[0]!=''#' ':#摆脱评论行
x = eval(行)
dicAnsMorph [x [0] [1]] = x [1] [1] #recid是密钥,解析字典是
值
但它每次都在x = eval(行)时崩溃。为什么是这样?如果我将
a.txt更改为:
#comments
[(''recId'',3),( ''解析'',{''pos'':''''''''''''''''''''}')
它工作正常。为什么它不适用于多行?它让我开车了
疯了!
Hi,
I have a text file called a.txt:
# comments
[(''recId'', 3), (''parse'', {''pos'': u''np'', ''gen'': u''m''})]
[(''recId'', 5), (''parse'', {''pos'': u''np'', ''gen'': u''m''})]
[(''recId'', 7 ), (''parse'', {''pos'': u''np'', ''gen'': u''m''})]
I read it using this:
filAnsMorph = codecs.open(''a.txt'', ''r'', ''utf-8'') # Initialise input
file
dicAnsMorph = {}
for line in filAnsMorph:
if line[0] != ''#'': # Get rid of comment lines
x = eval(line)
dicAnsMorph[x[0][1]] = x[1][1] # recid is key, parse dict is
value
But it crashes every time on x = eval(line). Why is this? If I change
a.txt to:
# comments
[(''recId'', 3), (''parse'', {''pos'': u''np'', ''gen'': u''m''})]
it works fine. Why doesn''t it work with multiple lines? it''s driving me
crazy!
尝试:
x = eval(line.strip(''\ n''))
-
bruno desthuilliers
python -c" print''@''。join([''。''。join([w p.split(''。'')中的w的[:: - 1]])''o **** @ xiludom.gro'中的
p。分裂(''@ '')])"
try with:
x = eval(line.strip(''\n''))
--
bruno desthuilliers
python -c "print ''@''.join([''.''.join([w[::-1] for w in p.split(''.'')]) for
p in ''o****@xiludom.gro''.split(''@'')])"
manstey写道:
manstey wrote:
那是行不通的。我刚收到一个错误:
x = eval(line.strip(''\ n''))
文件"< string>" ;,第1行
[(''recId'',3),(''解析'',{''pos'':你'''''''gen'':你好'}})
语法错误:解析时意外的EOF
That doesn''t work. I just get an error:
x = eval(line.strip(''\n''))
File "<string>", line 1
[(''recId'', 3), (''parse'', {''pos'': u''np'', ''gen'': u''m''})]
SyntaxError: unexpected EOF while parsing
是文件的最后一行空的??
用于
for filAnsMorph中的行:
#删除任何尾随和前导空格包括删除\ n
line = line.strip()
#删除注释行
if line.startswith(''#' '):
继续
#摆脱空行
如果行=='''':
继续
#do工作
x = eval(行)
NB默认情况下strip()从目标中删除前导和尾随字符
字符串。将whitspace定义为whitespace =''\ t \ n \ x0b \ x0c \ r''
Eric
is the last line of your file empty ??
what with
for line in filAnsMorph:
# remove any trailing and leading whitespace includes removing \n
line = line.strip()
# Get rid of comment lines
if line.startswith(''#''):
continue
# Get rid of blank line
if line == '''':
continue
#do the job
x = eval(line)
NB by default strip() removes leading and trailing characters from the target
string. with whitspace defined as whitespace = ''\t\n\x0b\x0c\r ''
Eric
这篇关于eval to dict问题NEWB疯了!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!