从 Python 到 F# 的字符串替换实用程序转换

发布于 2024-11-10 03:03:52 字数 1524 浏览 3 评论 0原文

我有一个简单的 python 实用程序代码,可以逐行修改字符串。代码如下。

import re

res = ""
with open("tclscript.do","r") as f:
    lines = f.readlines()
    for l in lines:
        l = l.rstrip()
        l = l.replace("{","{{")
        l = l.replace("}","}}")
        l = re.sub(r'#(\d+)', r'{\1}',l)
        l += r'\n'
        res += l
    res = "code="+res

with open("tclscript.txt","w") as f:
    f.write(res)

使用 F# 实现的实用程序会是什么样子?它的 LOC 能比这个 Python 版本更短并且更容易阅读吗?

添加了

python 代码处理 C# 字符串中的 tcl 脚本。 C#字符串中的'{'/'}'修改为'{{'/'}}','#'后面的数字修改为'{}'括起来的数字。例如,#1 -> {1}。

添加

这是工作示例

open System.IO
open System.Text.RegularExpressions

let lines = 
  File.ReadAllLines("tclscript.do")
  |> Seq.map (fun line ->
      let newLine = Regex.Replace(line.Replace("{", "{{").Replace("}", "}}"), @"#(\d+)", "{$1}") + @"\n"
      newLine )

let concatenatedLine = Seq.toArray lines |> String.concat ""
File.WriteAllText("tclscript.txt", concatenatedLine)

或者如这个答案。

open System.IO
open System.Text

let lines = 
  let re = System.Text.RegularExpressions.Regex(@"#(\d+)")
  [|for line in File.ReadAllLines("tclscript.do") ->
      re.Replace(line.Replace("{", "{{").Replace("}", "}}").Trim(), "$1", 1) + @"\n"|]

let concatenatedLine = lines |> String.concat ""
File.WriteAllText("tclscript.txt", concatenatedLine)

I have a simple python utility code that modifies the string line by line. The code is as follows.

import re

res = ""
with open("tclscript.do","r") as f:
    lines = f.readlines()
    for l in lines:
        l = l.rstrip()
        l = l.replace("{","{{")
        l = l.replace("}","}}")
        l = re.sub(r'#(\d+)', r'{\1}',l)
        l += r'\n'
        res += l
    res = "code="+res

with open("tclscript.txt","w") as f:
    f.write(res)

How can the utility implemented with F# would look like? Can it be shorter in LOC and easier to read than this Python version?

ADDED

The python code processes tcl script in C# string. The '{'/'}' in C# string should be changed into '{{'/'}}', and number after '#' should be modified to number enclosed by '{}'. For example, #1 -> {1}.

ADDED

This is the working example

open System.IO
open System.Text.RegularExpressions

let lines = 
  File.ReadAllLines("tclscript.do")
  |> Seq.map (fun line ->
      let newLine = Regex.Replace(line.Replace("{", "{{").Replace("}", "}}"), @"#(\d+)", "{$1}") + @"\n"
      newLine )

let concatenatedLine = Seq.toArray lines |> String.concat ""
File.WriteAllText("tclscript.txt", concatenatedLine)

Or as is explained in This answer.

open System.IO
open System.Text

let lines = 
  let re = System.Text.RegularExpressions.Regex(@"#(\d+)")
  [|for line in File.ReadAllLines("tclscript.do") ->
      re.Replace(line.Replace("{", "{{").Replace("}", "}}").Trim(), "$1", 1) + @"\n"|]

let concatenatedLine = lines |> String.concat ""
File.WriteAllText("tclscript.txt", concatenatedLine)

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

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

发布评论

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

评论(1

野の 2024-11-17 03:03:52

我不会给你 F# 版本的完整示例,因为我不确定你的 Python 版本中的正则表达式应该做什么。然而,一个好的 F# 解决方案的一般结构如下所示:

let lines = 
  File.ReadAllLines("tclscript.do")
  |> Seq.map (fun line ->
      let newLine = line.Replace("{", "{{").Replace("}", "}}")
      // Implement additional string processing here
      newLine )

File.WriteAllLines("tclscript.txt", lines)

由于您的代码片段是逐行工作的,因此我使用 ReadAllLines 将文件作为行列表读取,然后使用 Seq.map 将函数应用于每一行。可以使用 WriteAllLines 将新的行集合写入文件。

正如评论中提到的,我认为您可以在 Python 中编写几乎相同的内容(即无需显式连接字符串并使用一些高阶函数或理解语法来处理集合)。

I will not give you a complete example of the F# version, because I'm not sure what the regular expression in your Python version is supposed to do. However, the general structure of a nice F# solution would look something like this:

let lines = 
  File.ReadAllLines("tclscript.do")
  |> Seq.map (fun line ->
      let newLine = line.Replace("{", "{{").Replace("}", "}}")
      // Implement additional string processing here
      newLine )

File.WriteAllLines("tclscript.txt", lines)

Since your snippet is working line-by-line, I used ReadAllLines to read file as a list of lines and then used Seq.map to apply a function to every line. The new collection of lines can be written to a file using WriteAllLines.

As mentioned in the comment, I think that you can write pretty much the same thing in Python (i.e. without explicitly concatenating the string and using some higher-order function or comprehension syntax to process the collection).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文