-1
file_name = input("Enter the file name: ") #magicsquares.txt
#show users the table
infile = open(file_name, "r")
file = infile.readlines()
infile.close()
#converting the numbers in notepad into a list (integers)
table= []
for i in range (len(file)):
    row= file[i].split(' ')
for j in range (len(row)):
    row[j]= int(row[j])
table.append(row)
print (row)


#selecting cells
select1 = int(input("Enter row number: "))
select2= int(input("Enter column number: "))

This is a magic square with the list inside a notepad text file. How do I select the row and column in the list so that I can change the value of the specific coordinate in the list?

For example, the user inputs coordinate [0][1]. How do I locate that coordinate inside the notepad file and prompt the user to input an integer that replaces the original value at coordinate [0][1]?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
akama112
  • 3
  • 4
  • The problem is that i can't seem to select the coordinate, and ask the user to input a new integer value into the specific coordinate in the array. – akama112 Mar 19 '16 at 19:44
  • Then show a [mcve] of that; your code appears to stop before you even attempt it. – jonrsharpe Mar 19 '16 at 19:47
  • I think you need the second for loop to be nested for it to correctly make the table then I think you just need to do `table[select1][select2] = ...` – Tadhg McDonald-Jensen Mar 19 '16 at 20:00
  • i definitely agree.... was planning to construct the code starting with the cell selection first then only inserting the loops just to ensure everything is working step by step :) thanks! – akama112 Mar 20 '16 at 03:07

1 Answers1

1

Let's assume that your input file sample.txt has a list of numbers separated by a space ' ' contains the following:

1 2 3 4
5 6 7 8
9 0 1 2
1 4 7 9

The above is a matrix like structure.

The python code would look something like this:

import fileinput
import linecache

file_name = input("Enter the file name: ") #sample.txt

# selecting cells
select1 = int(input("Enter row number: ")) #0
select2 = int(input("Enter column number: ")) #1

# New value
newVal = int(input("Enter row number: ")) #100

# Get nth line from the input file and strip any newline chars
textToSearch = linecache.getline(file_name, select1+1).strip('\n')

# Transform to the line read to a list 
tmpList = [int(_) for _ in textToSearch.split(' ')]

# Replace the list with the new value and form the str line back again
tmpList[select2] =  newVal
textToReplace = ' '.join(str(_) for _ in tmpList)

# Modify the sample.txt file inplace 
with fileinput.FileInput(file_name, inplace=True, backup='.bak') as file:
    for line in file:
        print(line.replace(textToSearch, textToReplace), end='')

file.close()
linecache.clearcache()

The sample.txt should now look like this:

1 100 3 4
5 6 7 8
9 0 1 2
1 4 7 9

Note: This is specific to Python3

Important resources:

  1. linecache — Random access to text lines
  2. fileinput

Other SO QAs worth reading:

  1. How to search and replace text in a file using Python?
  2. Go to a specific line in Python?
Community
  • 1
  • 1
benSooraj
  • 447
  • 5
  • 18