在线咨询
eetop公众号 创芯大讲堂 创芯人才网
切换到宽版

EETOP 创芯网论坛 (原名:电子顶级开发网)

手机号码,快捷登录

手机号码,快捷登录

找回密码

  登录   注册  

快捷导航
搜帖子
查看: 8997|回复: 8

[讨论] tcl处理文本中的大括号{

[复制链接]
发表于 2012-6-23 11:42:03 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?注册

x
大家好,我在使用tcl一行一行处理某个文件中的文本时,如果这一行包含大括号{ ,好像就会报错,可能是因为大括号{在tcl中是属于关键字类型的,大家有什么好办法处理这种情况啊?谢谢
 楼主| 发表于 2012-7-6 09:50:02 | 显示全部楼层
回复 2# zhq415758192


    你好,在get $fileopen line 时,并不知道这行中是否有大括号{ ,如果没有,则不会有问题,如果有大括号{,则会报错。你说的\ 是怎么处理呢?
最近好像发现如果文件中的某一行中含有
xx "xxx".
  如果写了set a0 [lindex $line 0] 好像tcl也会报错,说什么引用的最后不是空格结尾,而是点。
我猜想,这可能是因为双引号"" 在tcl中是用来引用的(特殊的作用),所以在处理文本时可能会出错,这和前面的大括号{是类似的现象,难道这是tcl语言天生的bug?
发表于 2014-4-3 21:02:32 | 显示全部楼层
set data [open file.txt r]
set lines [split $data]
set num [llength $lines]
for {set i 0} {$i<=$num } {incr i +1 } {
set  linei [lindex $lines $i]
set line [split $linei " " ]
set a0 [lindex $line 0] #再对列表line进行处理貌似解决问题

}
发表于 2014-4-3 21:06:18 | 显示全部楼层
回复 3# jackiexiuyi


set output [open file.txt r] #打开读入文件set data [read $output]
set lines [split $data]
set num [llength $lines]
for {set i 0} {$i<=$num } {incr i +1 } {
set  linei [lindex $lines $i]
set line [split $linei " " ]
set a0 [lindex $line 0] #再对列表line进行处理貌似解决问题

}


不好意思上篇写漏了
发表于 2014-4-4 11:21:50 | 显示全部楼层
本帖最后由 sjtusonic 于 2014-4-4 11:26 编辑




    在处理文本时,得到$line变量后,
最好先用regsub过一遍正则表达式,
把一些不care的特殊符号删除掉。

参考这段代码:
(贴上来乱码了,你看https://www.tcl.tk/man/tcl8.6/TclCmd/regsub.htm的末尾吧。)
Convert all non-ASCII and Tcl-significant characters into \u escape sequences by using regsub and subst in combination:

# This RE is just a character class for almost everything "bad"set RE {[][{};#\\\$ \r\t\u0080-\uffff]}# We will substitute with a fragment of Tcl script in bracketsset substitution {[format \\\\u%04x [scan "\\&" %c]]}# Now we apply the substitution to get a subst-string that# will perform the computational parts of the conversion. Note# that newline is handled specially through string map since# backslash-newline is a special sequence.set quoted [subst [string map {\n {\\u000a}} \        [regsub -all $RE $string $substitution]]]
发表于 2014-4-6 22:39:06 | 显示全部楼层
使用转义字符啊
发表于 2014-4-10 11:52:13 | 显示全部楼层
tcl没有关键字,关键是 你的正则表达式 或者 字符串处理有一些细节
发表于 2015-7-20 14:30:45 | 显示全部楼层
使用转义字符
您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

站长推荐 上一条 /2 下一条

小黑屋| 手机版| 关于我们| 联系我们| 在线咨询| 隐私声明| EETOP 创芯网
( 京ICP备:10050787号 京公网安备:11010502037710 )

GMT+8, 2024-9-20 05:46 , Processed in 0.020347 second(s), 8 queries , Gzip On, Redis On.

eetop公众号 创芯大讲堂 创芯人才网
快速回复 返回顶部 返回列表