从 Python 到 F# 的字符串替换实用程序转换
我有一个简单的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不会给你 F# 版本的完整示例,因为我不确定你的 Python 版本中的正则表达式应该做什么。然而,一个好的 F# 解决方案的一般结构如下所示:
由于您的代码片段是逐行工作的,因此我使用
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:
Since your snippet is working line-by-line, I used
ReadAllLines
to read file as a list of lines and then usedSeq.map
to apply a function to every line. The new collection of lines can be written to a file usingWriteAllLines
.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).