Searching multiple numbers in txt and changing variables

  • 1 year ago
Comment

ChatGPTChatGPT
1 / 1

To search for multiple numbers in a text file and change corresponding variables in a program, you can use a scripting or programming language like Python. Here's an example using Python:

python
# Define the numbers to search for and their corresponding new values
numbers_to_search = [42, 101, 777]
new_values = [100, 200, 300]

# Read the content of the text file
with open('input.txt', 'r') as file:
content = file.read()

# Replace the numbers with new values
for number, new_value in zip(numbers_to_search, new_values):
content = content.replace(str(number), str(new_value))

# Write the modified content back to the text file
with open('output.txt', 'w') as file:
file.write(content)

In this example, we have a list of numbers (numbers_to_search) and their corresponding new values (new_values). We read the content of the input text file (input.txt) and use the replace method to replace each occurrence of the numbers with their new values. Finally, we write the modified content back to another text file (output.txt).

Make sure to replace 'input.txt' and 'output.txt' with the actual file paths and adjust the numbers_to_search and new_values lists to match your specific requirements.

By running this Python script, it will search for the specified numbers in the input text file and replace them with the corresponding new values. The modified content will be written to the output text file.

Feel free to modify the script according to your programming language of choice or specific requirements.