Replace indexed strings in a list and write out to a text file

how to search and replace text in a file using python
python replace specific line in file
python list replace
python replace strings in a text file
python replace file contents
python replace string in list
python find and replace in txt
string replace python

I'm attempting to replace certain words (essentially the 2nd word of each line) in a text file, and then write them back out to either a new file, or overwrite the existing file.

I thought I was making progress, but when I went to write to a new file, I got an error saying I couldn't write a list to a text file. I can't simply replace a word for another word, because I have an 'else' clause that covers any word that doesn't match the others I need to replace.

Here's an example of the text I'm trying to modify, this text is contained in a .txt file:

id int,
organization_id int,
billing_month date,
fee_type varchar(100),
rate float,
price float,
uom varchar(25),
amount float,
currency_code_id float,
process_ts timestamptz NOT NULL DEFAULT (now())::timestamptz(6)

I'd like to change:

'int' --> 'BIGINT'
'numeric' --> 'DOUBLE'
'float' --> 'DOUBLE'
ELSE other data type --> 'STRING' .  

In the original data, notice that some have other characters, such as "varchar(100)" - I'd like to replace those with "STRING" and eliminate the '(100)' piece as well.

And then either overwrite or create a new text file. So the above example output if replaced properly would be:

organization_id BIGINT,
billing_month STRING,
fee_type STRING,
rate DOUBLE,
price DOUBLE,
amount DOUBLE,
currency_code_id DOUBLE,
process_ts STRING

I'm having trouble knowing if I should be creating lists, and then modifying them, and then writing those lists to the text file, or dictionaries, or some other method I'm not thinking of. I'm very much a beginner so apologies if this isn't very clear.

Contents of txt.txt:

id int,
organization_id int,
billing_month date,
fee_type varchar(100),
rate float,
price float,
uom varchar(25),
amount float,
currency_code_id float,
process_ts timestamptz NOT NULL DEFAULT (now())::timestamptz(6)


with open('txt.txt', 'r') as f:
    text =

mapping = {'int':'BIGINT',

replaced_text = []
for line in text:
    # temporarily remove comma
    line = line.rstrip(',')
    split_line = line.split()
    other_text, dtype = split_line[0], split_line[1:]
    new_dtype = mapping.get(' '.join(dtype), 'STRING')
    new_line = '{} {},\n'.format(other_text, new_dtype)

with open('txt_replaced.txt', 'w') as f:

Contents of txt_replaced.txt:

organization_id BIGINT,
billing_month STRING,
fee_type STRING,
rate DOUBLE,
price DOUBLE,
amount DOUBLE,
currency_code_id DOUBLE,
process_ts STRING,

Globally replace text in several files, Strings.Unbounded.Index(Source => S, Pattern => Pattern); listFiles := "a.txt|b.​txt|c.txt" ; Define a list of files in the current working directory Similarly, there is no attempt to concatenate an output string to write in one go as  Python – Write String to Text File. Now you can save or write string to text a file in persistent data storage using Python. To write string to a Text File, follow these sequence of steps: Open file in write mode using open () function. Write string to the file using write () method. Close the file using close () method.

You can iterate through each line and use a dictionary to replace the values in the second position of each line. This works for lines of any length, as long as the text to replace is the second word.

#vals to replace
replace_vals = {'int':'BIGINT', 'numeric':'DOUBLE', 'float':'DOUBLE'}

#file we write to
with open('out.txt', 'w') as outfile:
  #file we read from
  with open ("in.txt", 'r') as infile:
    #check each line
    for line in infile:
      #split line into words
      words = line.split()
      #get the first word and then replace the second word, defaulting to STRING
      w = words[0] + " " + replace_vals.get(words[1], 'STRING')
      #add a final newline
      w += "\n"
      #print to file


Python – How to Replace String in File?, Open output file in write mode and handle it in text mode. For each line read from input file, replace the string and write to output file. Close both input and output  How to replace string in a line that matches a Learn more about MATLAB How to replace string in a line that matches a regular expression and write it to a file

Maybe something like this may help you:

import os, sys
from re import match as regexSearch

path = os.path.dirname(__file__)
myFile = open(os.path.join(path, "filename.txt"), "r")

regExpr = r"[\w_]+ ([\w\(\)\d:]+)(,|\s)"

with open(os.path.join(path, "newFile.txt"), "w") as f:
    for line in myFile.readlines():
        match = regexSearch(regExpr, line)
        if match:
            result =
            if "int" in result:
                f.write(line.replace(result, "BIGINT"))
            elif result in ["numeric", "float"]:
                f.write(line.replace(result, "DOUBLE"))
                f.write(line.replace(result, "STRING"))
            print("couldn't find something in line:\n", line)

Python, Here the replacement of a substring in list of string is performed. The replace method can be coupled with the list comprehension technique to achieve this Output : The original list : ['4', 'kg', 'butter', 'for', '40', 'bucks'] The list after If you like GeeksforGeeks and would like to contribute, you can also write an article using  function Find-InTextFile { <# .SYNOPSIS Performs a find (or replace) on a string in a text file or files. . EXAMPLE PS> Find-InTextFile -FilePath 'C:\MyFile.txt' -Find 'water' -Replace 'wine' Replaces all instances of the string 'water' into the string 'wine' in 'C:\MyFile.txt'. .

Chapter 6 – Manipulating Strings, Let's look at some of the ways Python lets you write, print, and access strings in The following print() call would print identical text but doesn't use a multiline string: a list and each character in the string as an item with a corresponding index. include everything from spam[0] to spam[4] , leaving out the space at index 5. Writing a List to a File in Python. Actually the methods I am going to discuss here are used for writing text to a file in Python. The examples I am using here discusses writing the list to file but you can use it to write any kind of text. string etc using the functions mentioned here. Method 1: Writing a list to a file line by line in Python

Manipulating Characters in a String (The Java™ Tutorials , You can get the character at a particular index within a string by invoking the code gets from the Niagara palindrome the substring that extends from index 11 up to, The String class does have four methods for replacing found characters or the use of lastIndexOf() and substring() to isolate different parts of a file name. These examples show various ways to write text to a file. The first two examples use static convenience methods on the System.IO.File class to write each element of any IEnumerable<string> and a string to a text file. Example 3 shows how to add text to a file when you have to process each line individually as you write to the file.

substr_replace - Manual, substr_replace — Replace text within a portion of a string The above example will output: I recently ran across a situation where I need to strip a heavily nested html list such that foreach($content->links->links as $index=>$link){ a lot of ways below that people are using to write their own string truncation functions,  There are countless other ways to replace strings in a text file with PowerShell. It's possible to use regular expressions with the replace operator to find complex strings, use regular expression groups and a whole lot more. The world is your oyster now, my friend!

  • Does the solution need to be in python? Perl could do the trick as well; it's more suited to string replacement.
  • yes I am 100% unfamiliar with Perl unfortunately so if this is possible in Python I'd like to stick to that
  • I just added the 'process_ts' to my list of strings to replace as well, because this is another edge case I need to consider. Not all of my string lines are simply 2 words long, if that matters
  • thanks, I just edited my original post to include "process_ts timestamptz NOT NULL DEFAULT (now())::timestamptz(6)" as a line I need to edit. Will this change your solution at all knowing that some lines of strings are more than 2 strings long? The fact that not all lines I need to edit are 2 strings long is giving me the most trouble
  • that will change it, i'll update my answer
  • Thanks for your help - I'm still having trouble with the 'process_ts' piece though, I need that to read: "process_ts STRING", without all of the other stuff in between (this stuff: "timestamptz NOT NULL DEFAULT (now())::timestamptz(6)") - is there a way to eliminate that?
  • i see, so the field name is always one string with no spaces, but the data type might have any number of spaces?
  • @phenderbender i've updated the code to handle the edge case
  • thanks for the reply but unfortunately the word to replace isn't always the 2nd word in this example, (particularly the process_ts piece).
  • Do you mean the word to replace might occur in, say, position 3, or do you mean that if there's more than two words, replace them all? i.e. will I ever get some var int
  • for cases (such as: "process_ts timestamptz NOT NULL DEFAULT (now())::timestamptz(6)" the 2nd word will let me know what I need to replace it with, and then I need to not only replace the 2nd word, but also any trailing text after that 2nd word as well. so, "process_ts timestamptz NOT NULL DEFAULT (now())::timestamptz(6)" would need to be "process_ts STRING" after being converted
  • Thank you @Nick Reed
  • Glad to help - please consider upvoting and accepting the answer if it addresses your question.