从文本文件中提取数据并将其写入其他地方

发布于 2024-11-02 08:17:31 字数 211 浏览 3 评论 0原文

如何读取文件并将其中的元素放入列表中并将内容写入其他文件?

文件内容

this is my house and it is very good
this is my village and it is the best 

goodbest 必须重复10 次。

如果可能的话帮助我

How can I read a file and put the elements in it to a list and write the contents to other file?

the file contents are

this is my house and it is very good
this is my village and it is the best 

good and best has to be repeated 10 times.

help me if possible

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尹雨沫 2024-11-09 08:17:31

你是不是有类似的意思?

set fi [open "filename"]
set fo [open "outputfile" w]
while {[gets $fi line]!=-1} {
  if {$line!=""} {
    for {set i 0} {$i<10} {incr i} {
      puts $fo $line
    }
  }
}
close $fi
close $fo

Did you mean something like that?

set fi [open "filename"]
set fo [open "outputfile" w]
while {[gets $fi line]!=-1} {
  if {$line!=""} {
    for {set i 0} {$i<10} {incr i} {
      puts $fo $line
    }
  }
}
close $fi
close $fo
打小就很酷 2024-11-09 08:17:31

你的问题不清楚。

您的意思是包含“好”或“最好”的行需要重复 10 次吗?

set fin [open infile r]
set fout [open outfile w]
while {[gets $fin line] != -1} {
    switch -glob -- $line {
        *good* -
        *best* {
            for {set i 1} {$i <= 10} {incr i} {
                puts $fout $line
            }
        }
        default {
            # what to do for lines NOT containing "good" or "best"?
        }
    }
}
close $fin
close $fout

如果您的意思是实际单词需要重复 10 次:

while {[gets $fin line] != -1} {
    puts $fout [regsub -all {\y(good|best)\y} $line {& & & & & & & & & &}]
}

regsub 命令的示例:

set str "these goods are good; that bestseller is the best"
puts [regsub -all {\y(good|best)\y} $str {& & &}]
# prints: these goods are good good good; that bestseller is the best best best

更新

根据您的评论,您可能需要:

array set values {good 10 best 20}
while {[gets $fin line] != -1} {
    regsub -all {\y(good|best)\y} $line {&-$values(&)} line
    puts $fout [subst -nobackslashes -nocommands $line]
}

Your question is unclear.

Do you mean the lines containing "good" or "best" need to be repeated 10 times?

set fin [open infile r]
set fout [open outfile w]
while {[gets $fin line] != -1} {
    switch -glob -- $line {
        *good* -
        *best* {
            for {set i 1} {$i <= 10} {incr i} {
                puts $fout $line
            }
        }
        default {
            # what to do for lines NOT containing "good" or "best"?
        }
    }
}
close $fin
close $fout

If you mean the actual words need to be repeated 10 times:

while {[gets $fin line] != -1} {
    puts $fout [regsub -all {\y(good|best)\y} $line {& & & & & & & & & &}]
}

An example for that regsub command:

set str "these goods are good; that bestseller is the best"
puts [regsub -all {\y(good|best)\y} $str {& & &}]
# prints: these goods are good good good; that bestseller is the best best best

Update

Based on your comments, you might want:

array set values {good 10 best 20}
while {[gets $fin line] != -1} {
    regsub -all {\y(good|best)\y} $line {&-$values(&)} line
    puts $fout [subst -nobackslashes -nocommands $line]
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文