0

I am trying to append the parameters passed to a function to a specific place in an existing text file. txt file:

query{
text:"",
source_language:"",
target_language:"",
},
data_type:[16],
params{
client:"xyz"
}

python:

def function(text,source_language,target_language):
     f = open("file.txt", "w");
     f.write( 'text:' + text + '\n' )
     f.write( 'source_language:' + source_language + '\n' )
     f.write( 'target_language:' + target_language + '\n' )
     f.close()

But, its not working. Is there a way to append the parameters directly into the file including " " and ,. I am trying to add just the parameters into the existing file with data at the specified position.

Hari Krishnan
  • 2,049
  • 2
  • 18
  • 29

2 Answers2

0

Solution

In revision to your comments, considering that this is only being applied to the example above and need only to alter those specific three lines, this will accomplish the task (included if location: in case you don't match keyword it won't erase your file by open('w')

def change_text(new_text):
    content[1] = list(content[1])
    y = list(new_text)
    content[1] = content[1][:6] + y  + content[1][-2:]
    content[1] = ''.join(content[1])

def change_source_l(new_source_l):
    content[2] = list(content[2])
    y = list(new_source_l)
    content[2] = content[2][:17] + y  + content[2][-2:]
    content[2] = ''.join(content[2])

def change_target_l(new_target_l):
    content[3] = list(content[3])
    y = list(new_target_l)
    content[3] = content[3][:17] + y  + content[3][-2:]
    content[3] = ''.join(content[3])

filename = open('query.txt', 'r')
content = filename.read()
content = content.split()
filename.close()

name = open('query.txt', 'w')

change_text('something')
change_source_l('this')
change_target_l('that')

name.write('\n'.join(content))
name.close()

Output

(xenial)vash@localhost:~/python/LPTHW$ cat query.txt 
query{
text:"something",
source_language:"this",
target_language:"that",
},
data_type:[16],
params{
client:"xyz"
vash_the_stampede
  • 4,590
  • 1
  • 8
  • 20
  • Hi Vash, I just want the three input parameters given, to get attached between the double quotes mentioned in the file. Every time, the file should get overwritten. – sangeetha ramesh Sep 11 '18 at 17:24
  • Okay so instead of inserting you want to replace the entire line as in `text:"",` will become `text:"some_text"`,? instead of adding to the already blank entry `"",` and that goes for all three variables you described – vash_the_stampede Sep 11 '18 at 17:26
  • Can we assume that the lines we are exchanging are always as shown in order `text:, source_language:, and target_language` to say is this specific for this is example or has to work with different parameters and locations? – vash_the_stampede Sep 11 '18 at 17:29
  • I didn't get the output I wanted for the above code. Yes, the lines are to be in order. I don't want the entire content replaced, just content written over inside the double quotes every time new parameters are passed. – sangeetha ramesh Sep 11 '18 at 23:24
  • Okay not home I can do that though – vash_the_stampede Sep 12 '18 at 00:30
  • still there I think I got what you need! – vash_the_stampede Sep 12 '18 at 18:51
-1

Open file in r+ mode
Use .seek method from Python file I/O and then write your content.

PurushothamC
  • 111
  • 8
  • This will not insert content into a file. Rather, it will overwrite the existing content so you'll end up with corrupted file content. – blhsing Sep 11 '18 at 06:11
  • Thanks, this is something that i was looking for, but I can't seem to wrap my mind as to how to insert it inside the double quotes using seek. Any suggestion? – sangeetha ramesh Sep 11 '18 at 17:32