楼主: mowuse
807 7

[问答] 求助程序报错 [推广有奖]

  • 0关注
  • 2粉丝

大专生

98%

还不是VIP/贵宾

-

威望
0
论坛币
9934 个
通用积分
10.8376
学术水平
3 点
热心指数
0 点
信用等级
0 点
经验
14700 点
帖子
42
精华
0
在线时间
66 小时
注册时间
2009-11-5
最后登录
2022-5-5

楼主
mowuse 学生认证  发表于 2018-10-4 18:12:50 |只看作者 |坛友微信交流群|倒序 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
请大神指导下:

python代码如下:
import fileinput,re
# Matches fields enclosed in square brackets:
field_pat = re.compile(r'\[(.+?)\]')
# We'll collect variables in this:
scope = {}
# This is used in re.sub:
def replacement(match):
code = match.group(1)
try:
  # If the field can be evaluated, return it:
  return str(eval(code, scope))
except SyntaxError:
  # Otherwise, execute the assignment in the same scope ... exec code in scope
  # ... and return an empty string:
  return ''
# Get all the text as a single string:
# (There are other ways of doing this; see Chapter 11)
lines = []
for line in fileinput.input():
lines.append(line)
text = ''.join(lines)
# Substitute all the occurrences of the field pattern:
x=[]
print(field_pat.sub(replacement, text))


然后报错信息:
c:\python>python hello.py   text1.txt
Traceback (most recent call last):
  File "hello.py", line 28, in <module>
    print(field_pat.sub(replacement, text))
  File "hello.py", line 15, in replacement
    return str(eval(code, scope))
  File "<string>", line 1, in <module>
NameError: name 'x' is not defined

其中text1.txt中的内容:
[x=2]
[y=3]
The sum of [x] and [y] is [x + y].


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:replacement assignment substitute Occurrence placement

沙发
齐物论pi 学生认证  发表于 2018-10-5 16:02:43 来自手机 |只看作者 |坛友微信交流群
你这个就是说X没有定义,

使用道具

藤椅
mowuse 学生认证  发表于 2018-10-5 17:23:38 来自手机 |只看作者 |坛友微信交流群
齐物论pi 发表于 2018-10-5 16:02
你这个就是说X没有定义,
怎么改?

使用道具

板凳
齐物论pi 学生认证  发表于 2018-10-5 17:28:59 |只看作者 |坛友微信交流群
你把代码发给我,或者加我的机器学习群:347947372

使用道具

报纸
longgb246 学生认证  发表于 2018-10-5 17:54:49 |只看作者 |坛友微信交流群
是要执行text1.txt中的[x=2][y=3]并替换The sum of [x] and [y] is [x + y].吗

  1.     import fileinput, re

  2.     # Matches fields enclosed in square brackets:
  3.     field_pat = re.compile(r'\[(.+?)\]')
  4.     # We'll collect variables in this:
  5.     scope = {}

  6.     # This is used in re.sub:
  7.     def replacement(match):
  8.         code = match.group(1)
  9.         try:
  10.             # If the field can be evaluated, return it:
  11.             return str(eval(code, scope))
  12.         except SyntaxError:
  13.             # Otherwise, execute the assignment in the same scope ... exec code in scope
  14.             rep_exec = code.split('=')
  15.             scope.update({rep_exec[0].strip(): rep_exec[1].strip()})
  16.             # ... and return an empty string:
  17.             return ''

  18.     # Get all the text as a single string:
  19.     # (There are other ways of doing this; see Chapter 11)
  20.     lines = []
  21.     for line in fileinput.input():
  22.         lines.append(line)
  23.     text = ''.join(lines)
  24.     # Substitute all the occurrences of the field pattern:
  25.     x = []
  26.     print(field_pat.sub(replacement, text))
复制代码


你试试。

使用道具

地板
longgb246 学生认证  发表于 2018-10-5 17:56:23 |只看作者 |坛友微信交流群
longgb246 发表于 2018-10-5 17:54
是要执行text1.txt中的[x=2][y=3]并替换The sum of [x] and [y] is [x + y].吗
其他地方没有改过,不用复制过去,就只是# Otherwise下面加了2句代码

使用道具

7
mowuse 学生认证  发表于 2018-10-5 18:43:41 来自手机 |只看作者 |坛友微信交流群
longgb246 发表于 2018-10-5 17:56
其他地方没有改过,不用复制过去,就只是# Otherwise下面加了2句代码
加什么

使用道具

8
longgb246 学生认证  发表于 2018-10-5 23:03:29 |只看作者 |坛友微信交流群
mowuse 发表于 2018-10-5 18:43
加什么
我发的代码跟你的对比一下,自己看吧

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-20 05:52