String一个Python的替换工具转换为F# [英] String replace utility conversion from Python to F#
问题描述
我有一个简单的Python程序code表示修改一行的串线。在code是如下。
进口重
RES =
开放(tclscript.do,R)为f:
线= f.readlines()
对于L的线路:
L = l.rstrip()
升= l.replace({,{{)
升= l.replace(},}})
升=应用re.sub(R'#(\ D +)'中,R'{\ 1},升)
L + = R'\ N'
RES + = L
RES =code =+资源
开放(tclscript.txt,W)为f:
f.write(RES)
如何才能实现实用与F#会是什么样子?它可以在更短的LOC,更易于阅读比这个Python版本?
ADDED
蟒蛇code处理Tcl脚本在C#中的字符串。的在C#字符串'{'/'}'应该被改变为{{'/'}}'和后#号应该被修改为通过封闭号码{}。例如,#1 - > {1}。
ADDED
这是工作的例子
开System.IO
开放System.Text.RegularEx pressions
让线=
File.ReadAllLines(tclscript.do)
|> Seq.map(有趣行 - >
让换行符= Regex.Replace(line.Replace({,{{).Replace(},}}),@#(\ D +),{$ 1})+ @ \ N
新行)
让concatenatedLine = Seq.toArray线|> String.concat
File.WriteAllText(tclscript.txt,concatenatedLine)
或在<一个解释href="http://stackoverflow.com/questions/6156358/converting-seqstring-to-string-in-f/6156512#6156512">This回答。
开System.IO
开System.Text
让线=
让我们再次= System.Text.RegularEx pressions.Regex(@#(\ D +))
[|在File.ReadAllLines行(tclscript.do) - &GT;
re.Replace(line.Replace({,{{).Replace(},}})修剪(),$ 1,1)+ @\ N|]
让concatenatedLine =行|&GT; String.concat
File.WriteAllText(tclscript.txt,concatenatedLine)
我不会给你的F#版本一个完整的例子,因为我不知道什么正规的前pression在你的Python版本应该去做。然而,一个漂亮的F#的解决方案的总体结构将是这个样子:
让行=
File.ReadAllLines(tclscript.do)
|&GT; Seq.map(有趣行 - &GT;
让换行符= line.Replace({,{{).Replace(},}})
//这里实现额外的字符串处理
新行)
File.WriteAllLines(tclscript.txt,行)
由于您的代码段正在行由行,我用 ReadAllLines
来读取文件行的列表,然后用 Seq.map
来一个功能应用到每一行。线的新集合可以使用写入文件 WriteAllLines
。
正如评论,我觉得你可以写pretty的多少在Python同样的事情(即没有明确的串联字符串,并使用一些高阶函数或COM prehension语法处理集合)。
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)
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).
这篇关于String一个Python的替换工具转换为F#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!