python function variable in text












1















I'm trying to create a simple python function that looks like:



def fnc_assign(y,a): 
'new_acc_no' + y + '.text' = import_list[x][a]


What I want to do with it is to assign new_acc_no10.text = import_list[1][0] when I call fnc_assign(10,1) but I'm stuck on getting the left hand side of the function to work (I get a SyntaxError: can't assign to operator).



Thanks for your help!



edit:
As mentioned below, new_acc_no1 has a property .text that = import_list[x][0].



I have a lot of new_acc_no's so what I have been doing is:



new_acc_no1.text = import_list[x][0]
new_acc_no2.text = import_list[x][1]
new_acc_no3.text = import_list[x][2]
new_acc_no4.text = import_list[x][12]
new_acc_no5.text = import_list[x][13]


And I still have a lot more that I need to add so I thought writing a function that can write out new_acc_noY.text = import_list[x][a] would be beneficial with x being the variable in a while loop that the previous code block is in. So the function would be fnc_assign(1,2) and it will generate the output:



new_acc_no1.text = import_list[x][2]


with x still taking in the variable input from the loop its in. Is this possible?










share|improve this question




















  • 3





    Why do you want to dynamically create a variable name? Also, you can't have a period in a variable name in Python.

    – Havan Agrawal
    Nov 15 '18 at 4:17






  • 2





    Is .text supposed to be a property of new_acc_no<y>? Because if so, you need to go about this in a whole different way.

    – e.maguire
    Nov 15 '18 at 4:23













  • Yeah it's a property, please see post edit for additional info

    – Andy
    Nov 15 '18 at 4:43


















1















I'm trying to create a simple python function that looks like:



def fnc_assign(y,a): 
'new_acc_no' + y + '.text' = import_list[x][a]


What I want to do with it is to assign new_acc_no10.text = import_list[1][0] when I call fnc_assign(10,1) but I'm stuck on getting the left hand side of the function to work (I get a SyntaxError: can't assign to operator).



Thanks for your help!



edit:
As mentioned below, new_acc_no1 has a property .text that = import_list[x][0].



I have a lot of new_acc_no's so what I have been doing is:



new_acc_no1.text = import_list[x][0]
new_acc_no2.text = import_list[x][1]
new_acc_no3.text = import_list[x][2]
new_acc_no4.text = import_list[x][12]
new_acc_no5.text = import_list[x][13]


And I still have a lot more that I need to add so I thought writing a function that can write out new_acc_noY.text = import_list[x][a] would be beneficial with x being the variable in a while loop that the previous code block is in. So the function would be fnc_assign(1,2) and it will generate the output:



new_acc_no1.text = import_list[x][2]


with x still taking in the variable input from the loop its in. Is this possible?










share|improve this question




















  • 3





    Why do you want to dynamically create a variable name? Also, you can't have a period in a variable name in Python.

    – Havan Agrawal
    Nov 15 '18 at 4:17






  • 2





    Is .text supposed to be a property of new_acc_no<y>? Because if so, you need to go about this in a whole different way.

    – e.maguire
    Nov 15 '18 at 4:23













  • Yeah it's a property, please see post edit for additional info

    – Andy
    Nov 15 '18 at 4:43
















1












1








1








I'm trying to create a simple python function that looks like:



def fnc_assign(y,a): 
'new_acc_no' + y + '.text' = import_list[x][a]


What I want to do with it is to assign new_acc_no10.text = import_list[1][0] when I call fnc_assign(10,1) but I'm stuck on getting the left hand side of the function to work (I get a SyntaxError: can't assign to operator).



Thanks for your help!



edit:
As mentioned below, new_acc_no1 has a property .text that = import_list[x][0].



I have a lot of new_acc_no's so what I have been doing is:



new_acc_no1.text = import_list[x][0]
new_acc_no2.text = import_list[x][1]
new_acc_no3.text = import_list[x][2]
new_acc_no4.text = import_list[x][12]
new_acc_no5.text = import_list[x][13]


And I still have a lot more that I need to add so I thought writing a function that can write out new_acc_noY.text = import_list[x][a] would be beneficial with x being the variable in a while loop that the previous code block is in. So the function would be fnc_assign(1,2) and it will generate the output:



new_acc_no1.text = import_list[x][2]


with x still taking in the variable input from the loop its in. Is this possible?










share|improve this question
















I'm trying to create a simple python function that looks like:



def fnc_assign(y,a): 
'new_acc_no' + y + '.text' = import_list[x][a]


What I want to do with it is to assign new_acc_no10.text = import_list[1][0] when I call fnc_assign(10,1) but I'm stuck on getting the left hand side of the function to work (I get a SyntaxError: can't assign to operator).



Thanks for your help!



edit:
As mentioned below, new_acc_no1 has a property .text that = import_list[x][0].



I have a lot of new_acc_no's so what I have been doing is:



new_acc_no1.text = import_list[x][0]
new_acc_no2.text = import_list[x][1]
new_acc_no3.text = import_list[x][2]
new_acc_no4.text = import_list[x][12]
new_acc_no5.text = import_list[x][13]


And I still have a lot more that I need to add so I thought writing a function that can write out new_acc_noY.text = import_list[x][a] would be beneficial with x being the variable in a while loop that the previous code block is in. So the function would be fnc_assign(1,2) and it will generate the output:



new_acc_no1.text = import_list[x][2]


with x still taking in the variable input from the loop its in. Is this possible?







python function string-concatenation






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 4:48







Andy

















asked Nov 15 '18 at 4:14









AndyAndy

537




537








  • 3





    Why do you want to dynamically create a variable name? Also, you can't have a period in a variable name in Python.

    – Havan Agrawal
    Nov 15 '18 at 4:17






  • 2





    Is .text supposed to be a property of new_acc_no<y>? Because if so, you need to go about this in a whole different way.

    – e.maguire
    Nov 15 '18 at 4:23













  • Yeah it's a property, please see post edit for additional info

    – Andy
    Nov 15 '18 at 4:43
















  • 3





    Why do you want to dynamically create a variable name? Also, you can't have a period in a variable name in Python.

    – Havan Agrawal
    Nov 15 '18 at 4:17






  • 2





    Is .text supposed to be a property of new_acc_no<y>? Because if so, you need to go about this in a whole different way.

    – e.maguire
    Nov 15 '18 at 4:23













  • Yeah it's a property, please see post edit for additional info

    – Andy
    Nov 15 '18 at 4:43










3




3





Why do you want to dynamically create a variable name? Also, you can't have a period in a variable name in Python.

– Havan Agrawal
Nov 15 '18 at 4:17





Why do you want to dynamically create a variable name? Also, you can't have a period in a variable name in Python.

– Havan Agrawal
Nov 15 '18 at 4:17




2




2





Is .text supposed to be a property of new_acc_no<y>? Because if so, you need to go about this in a whole different way.

– e.maguire
Nov 15 '18 at 4:23







Is .text supposed to be a property of new_acc_no<y>? Because if so, you need to go about this in a whole different way.

– e.maguire
Nov 15 '18 at 4:23















Yeah it's a property, please see post edit for additional info

– Andy
Nov 15 '18 at 4:43







Yeah it's a property, please see post edit for additional info

– Andy
Nov 15 '18 at 4:43














2 Answers
2






active

oldest

votes


















0














change y to str(y)



def fnc_assign(y,x): 
import_list[x][0] = 'new_acc_no' + str(y) + '.text'


edit:



you can't add int to str.






share|improve this answer


























  • While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer.

    – hellow
    Nov 15 '18 at 7:47



















0














Should be



def fnc_assign(y,x): 
import_list[x][0] = 'new_acc_no' + y + '.text'


Note that your LHS is a string, and strings in Python are immutable, i.e. cannot be changed. I understood your intent is to assign the resulting string into the array element in question which you put on the RHS. Just reversing them does the assignment.



Or is your intent to actually have a variable named new_acc_no10.text and assign to it the value in the import list?



UPDATE



This also assumes y is a string (or supports concatenation with strings using the + operator), if not, use str(y) instead.





NEW TASK UPDATE



Your code in the question (assuming an outer loop in x



for x in someValueIterator:
new_acc_no1.text = import_list[x][0]
new_acc_no2.text = import_list[x][1]
new_acc_no3.text = import_list[x][2]
new_acc_no4.text = import_list[x][12]
new_acc_no5.text = import_list[x][13]


seems to suggest the following approach. Organize the account numbers into a dictionary, indexed by the RHS index you are using to assign, i.e. something like



acc_nos = [new_acc_no1, new_acc_no2, new_acc_no3, new_acc_no4, new_acc_no5]
indices = [0,1,2,12,13]
idx2acc = dict(zip(indices, acc_nos))


and now loop like this:



for index, acc_no in idx2acc.iteritems():
acc_no.text = import_list[x][index]


and taking into account the outer loop in x, instead of



for x in someValueIterator:
for index, acc_no in idx2acc.iteritems():
acc_no.text = import_list[x][index]


consider looping like so directly:



for current_list in import_list:
for index, acc_no in idx2acc.iteritems():
acc_no.text = current_list[index]





share|improve this answer


























  • Yeah sorry, it seems like I may have misjudged the issue, .text is a property of the new_acc_no

    – Andy
    Nov 15 '18 at 4:50











  • @Andy see update

    – gt6989b
    Nov 15 '18 at 16:56











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312351%2fpython-function-variable-in-text%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














change y to str(y)



def fnc_assign(y,x): 
import_list[x][0] = 'new_acc_no' + str(y) + '.text'


edit:



you can't add int to str.






share|improve this answer


























  • While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer.

    – hellow
    Nov 15 '18 at 7:47
















0














change y to str(y)



def fnc_assign(y,x): 
import_list[x][0] = 'new_acc_no' + str(y) + '.text'


edit:



you can't add int to str.






share|improve this answer


























  • While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer.

    – hellow
    Nov 15 '18 at 7:47














0












0








0







change y to str(y)



def fnc_assign(y,x): 
import_list[x][0] = 'new_acc_no' + str(y) + '.text'


edit:



you can't add int to str.






share|improve this answer















change y to str(y)



def fnc_assign(y,x): 
import_list[x][0] = 'new_acc_no' + str(y) + '.text'


edit:



you can't add int to str.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 15 '18 at 8:21

























answered Nov 15 '18 at 4:18









RecessiveRecessive

310111




310111













  • While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer.

    – hellow
    Nov 15 '18 at 7:47



















  • While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer.

    – hellow
    Nov 15 '18 at 7:47

















While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer.

– hellow
Nov 15 '18 at 7:47





While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer.

– hellow
Nov 15 '18 at 7:47













0














Should be



def fnc_assign(y,x): 
import_list[x][0] = 'new_acc_no' + y + '.text'


Note that your LHS is a string, and strings in Python are immutable, i.e. cannot be changed. I understood your intent is to assign the resulting string into the array element in question which you put on the RHS. Just reversing them does the assignment.



Or is your intent to actually have a variable named new_acc_no10.text and assign to it the value in the import list?



UPDATE



This also assumes y is a string (or supports concatenation with strings using the + operator), if not, use str(y) instead.





NEW TASK UPDATE



Your code in the question (assuming an outer loop in x



for x in someValueIterator:
new_acc_no1.text = import_list[x][0]
new_acc_no2.text = import_list[x][1]
new_acc_no3.text = import_list[x][2]
new_acc_no4.text = import_list[x][12]
new_acc_no5.text = import_list[x][13]


seems to suggest the following approach. Organize the account numbers into a dictionary, indexed by the RHS index you are using to assign, i.e. something like



acc_nos = [new_acc_no1, new_acc_no2, new_acc_no3, new_acc_no4, new_acc_no5]
indices = [0,1,2,12,13]
idx2acc = dict(zip(indices, acc_nos))


and now loop like this:



for index, acc_no in idx2acc.iteritems():
acc_no.text = import_list[x][index]


and taking into account the outer loop in x, instead of



for x in someValueIterator:
for index, acc_no in idx2acc.iteritems():
acc_no.text = import_list[x][index]


consider looping like so directly:



for current_list in import_list:
for index, acc_no in idx2acc.iteritems():
acc_no.text = current_list[index]





share|improve this answer


























  • Yeah sorry, it seems like I may have misjudged the issue, .text is a property of the new_acc_no

    – Andy
    Nov 15 '18 at 4:50











  • @Andy see update

    – gt6989b
    Nov 15 '18 at 16:56
















0














Should be



def fnc_assign(y,x): 
import_list[x][0] = 'new_acc_no' + y + '.text'


Note that your LHS is a string, and strings in Python are immutable, i.e. cannot be changed. I understood your intent is to assign the resulting string into the array element in question which you put on the RHS. Just reversing them does the assignment.



Or is your intent to actually have a variable named new_acc_no10.text and assign to it the value in the import list?



UPDATE



This also assumes y is a string (or supports concatenation with strings using the + operator), if not, use str(y) instead.





NEW TASK UPDATE



Your code in the question (assuming an outer loop in x



for x in someValueIterator:
new_acc_no1.text = import_list[x][0]
new_acc_no2.text = import_list[x][1]
new_acc_no3.text = import_list[x][2]
new_acc_no4.text = import_list[x][12]
new_acc_no5.text = import_list[x][13]


seems to suggest the following approach. Organize the account numbers into a dictionary, indexed by the RHS index you are using to assign, i.e. something like



acc_nos = [new_acc_no1, new_acc_no2, new_acc_no3, new_acc_no4, new_acc_no5]
indices = [0,1,2,12,13]
idx2acc = dict(zip(indices, acc_nos))


and now loop like this:



for index, acc_no in idx2acc.iteritems():
acc_no.text = import_list[x][index]


and taking into account the outer loop in x, instead of



for x in someValueIterator:
for index, acc_no in idx2acc.iteritems():
acc_no.text = import_list[x][index]


consider looping like so directly:



for current_list in import_list:
for index, acc_no in idx2acc.iteritems():
acc_no.text = current_list[index]





share|improve this answer


























  • Yeah sorry, it seems like I may have misjudged the issue, .text is a property of the new_acc_no

    – Andy
    Nov 15 '18 at 4:50











  • @Andy see update

    – gt6989b
    Nov 15 '18 at 16:56














0












0








0







Should be



def fnc_assign(y,x): 
import_list[x][0] = 'new_acc_no' + y + '.text'


Note that your LHS is a string, and strings in Python are immutable, i.e. cannot be changed. I understood your intent is to assign the resulting string into the array element in question which you put on the RHS. Just reversing them does the assignment.



Or is your intent to actually have a variable named new_acc_no10.text and assign to it the value in the import list?



UPDATE



This also assumes y is a string (or supports concatenation with strings using the + operator), if not, use str(y) instead.





NEW TASK UPDATE



Your code in the question (assuming an outer loop in x



for x in someValueIterator:
new_acc_no1.text = import_list[x][0]
new_acc_no2.text = import_list[x][1]
new_acc_no3.text = import_list[x][2]
new_acc_no4.text = import_list[x][12]
new_acc_no5.text = import_list[x][13]


seems to suggest the following approach. Organize the account numbers into a dictionary, indexed by the RHS index you are using to assign, i.e. something like



acc_nos = [new_acc_no1, new_acc_no2, new_acc_no3, new_acc_no4, new_acc_no5]
indices = [0,1,2,12,13]
idx2acc = dict(zip(indices, acc_nos))


and now loop like this:



for index, acc_no in idx2acc.iteritems():
acc_no.text = import_list[x][index]


and taking into account the outer loop in x, instead of



for x in someValueIterator:
for index, acc_no in idx2acc.iteritems():
acc_no.text = import_list[x][index]


consider looping like so directly:



for current_list in import_list:
for index, acc_no in idx2acc.iteritems():
acc_no.text = current_list[index]





share|improve this answer















Should be



def fnc_assign(y,x): 
import_list[x][0] = 'new_acc_no' + y + '.text'


Note that your LHS is a string, and strings in Python are immutable, i.e. cannot be changed. I understood your intent is to assign the resulting string into the array element in question which you put on the RHS. Just reversing them does the assignment.



Or is your intent to actually have a variable named new_acc_no10.text and assign to it the value in the import list?



UPDATE



This also assumes y is a string (or supports concatenation with strings using the + operator), if not, use str(y) instead.





NEW TASK UPDATE



Your code in the question (assuming an outer loop in x



for x in someValueIterator:
new_acc_no1.text = import_list[x][0]
new_acc_no2.text = import_list[x][1]
new_acc_no3.text = import_list[x][2]
new_acc_no4.text = import_list[x][12]
new_acc_no5.text = import_list[x][13]


seems to suggest the following approach. Organize the account numbers into a dictionary, indexed by the RHS index you are using to assign, i.e. something like



acc_nos = [new_acc_no1, new_acc_no2, new_acc_no3, new_acc_no4, new_acc_no5]
indices = [0,1,2,12,13]
idx2acc = dict(zip(indices, acc_nos))


and now loop like this:



for index, acc_no in idx2acc.iteritems():
acc_no.text = import_list[x][index]


and taking into account the outer loop in x, instead of



for x in someValueIterator:
for index, acc_no in idx2acc.iteritems():
acc_no.text = import_list[x][index]


consider looping like so directly:



for current_list in import_list:
for index, acc_no in idx2acc.iteritems():
acc_no.text = current_list[index]






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 15 '18 at 16:56

























answered Nov 15 '18 at 4:16









gt6989bgt6989b

1,65262547




1,65262547













  • Yeah sorry, it seems like I may have misjudged the issue, .text is a property of the new_acc_no

    – Andy
    Nov 15 '18 at 4:50











  • @Andy see update

    – gt6989b
    Nov 15 '18 at 16:56



















  • Yeah sorry, it seems like I may have misjudged the issue, .text is a property of the new_acc_no

    – Andy
    Nov 15 '18 at 4:50











  • @Andy see update

    – gt6989b
    Nov 15 '18 at 16:56

















Yeah sorry, it seems like I may have misjudged the issue, .text is a property of the new_acc_no

– Andy
Nov 15 '18 at 4:50





Yeah sorry, it seems like I may have misjudged the issue, .text is a property of the new_acc_no

– Andy
Nov 15 '18 at 4:50













@Andy see update

– gt6989b
Nov 15 '18 at 16:56





@Andy see update

– gt6989b
Nov 15 '18 at 16:56


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312351%2fpython-function-variable-in-text%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Lugert, Oklahoma