Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5964687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:28:41+00:00 2026-05-22T19:28:41+00:00

I have a simple python utility code that modifies the string line by line.

  • 0

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)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T19:28:42+00:00Added an answer on May 22, 2026 at 7:28 pm

    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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following simple Python code that makes a simple post request to
I have a simple Python script that I want to stop executing if a
I have a simple Python script that uses the socket module to send a
I have a simple python program that I'd like to daemonize. Since the point
I have a very simple python script that should scan a text file, which
I have the following simple python test script that uses Suds to call a
I have a fairly simple python loop that calls a few functions, and writes
Writing a test app to emulate PIO lines, I have a very simple Python/Tk
I have 2 simple questions about python: 1.How to get number of lines of
I have a simple form in python + pylons that submits to a controller.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.