I am a novice.
I want to replace a content with a variable
How do I do that ?

Thanks

You mean something like this ...

a = 1
b = 2
c = 3
my_list = [a, b, c, a + c]

# test it
print my_list  # [1, 2, 3, 4]

... or ...

my_list = [1, 2, 3, 4]
a, b, c, d = my_list

# test it
print a, b, c, d  # 1 2 3 4

Let me explain the case,

I have a file called AA which contains variable values as below;
file1
foot1
boby
...

I have another file called BB which contains variable values like this,
abc12
cba11
def123
...
Also I have another file called CC which will be assigned some variable values as following;

Server variable1
Client variable2


I have to assign the variables in the file of AA to variable1 in the CC and assign the variables in the file of BB to variable2 in the CC.
Finally I should see as following in a file called DD
Server file1
Client abc12

Server foot1
Client cba11

Server boby
Client def123

How can I do that ?
Thanks

Let's create the needed data files for testing ...

# write the test data files

def write_datafile(filename, data):
    fout = open(filename, "w")
    fout.write(data)
    fout.close()
    print "data written to file", filename

data1 = """file1
foot1
boby"""

data2 = """abc12
cba11
def123"""

data3 = """Server variable1
Client variable2"""

write_datafile("data1.txt", data1)
write_datafile("data2.txt", data2)
write_datafile("data3.txt", data3)

This may be one way you can accomplish your goal ...

# read the test data files, process, and write combined file

def read_datafile(filename):
    """read a text file lines to a list"""
    fin = open(filename, "r")
    data_list = fin.readlines()
    fin.close()
    print filename, "has been read to data list"
    # process the data_list, remove trailing \n
    data_list = [item.rstrip() for item in data_list]
    return data_list

def write_datafile(filename, data_list):
    """write the data list to a line file"""
    fout = open(filename, "w")
    fout.writelines(data_list)
    fout.close()
    print "data list written to file", filename


dlist1 = read_datafile("data1.txt")
dlist2 = read_datafile("data2.txt")
dlist3 = read_datafile("data3.txt")

# test the lists
print dlist1  # ['file1', 'foot1', 'boby']
print dlist2  # ['abc12', 'cba11', 'def123']
print dlist3  # ['Server variable1', 'Client variable2']

# extract the names from dlist3
dlist3_names = [item.split()[0] for item in dlist3]
# test it ...
print dlist3_names  # ['Server', 'Client']

# create the final data list
# note that variable1 and variable2 are fixed to dlist1 and dlist2
dlist4 = []
# note dlist1 and dlist2 need to have the same length
for d in range(len(dlist1)):
    server_string = "%s %s\n" % (dlist3_names[0], dlist1[d])
    dlist4.append(server_string)
    client_string = "%s %s\n" % (dlist3_names[1], dlist2[d])
    dlist4.append(client_string)
    # optional empty line
    dlist4.append('\n')
    
# test it ...
print dlist4

"""
my beautified output ...
['Server file1\n', 'Client abc12\n', '\n',
'Server foot1\n', 'Client cba11\n', '\n',
'Server boby\n', 'Client def123\n', '\n']
"""

write_datafile("data4.txt", dlist4)

Now you can look at the data4.txt file with an editor.

The values I gave as an example are a variable values. That's to say, the contains of AA and BB are variable. So They haven't fixed values.

It looks like fixed means that variable1 value has to come from dlist1 and is fixed to the Server and variable2 value comes from dlist2 and is fixed to the Client. At least that is the way I understand this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.