How to convert the tuple elements to string, without losing the tuple properties?












-1















I created a tuple by reading data from a MySQL table. All the elements are of mixed data types and to be able to apply few string operations (upper case, removal of special characters etc), I need to convert all those elements to string.



I tried "".str() and .join() but the resultant is a pure string and I lose information about individual elements.



Something like:



(ABC, XYZ, 234, QWE, 578)   <-- mixed datatypes but I can do tuple[0] to just fetch ABC


The cursor returns multiple records. The struct_address_str[0] returns the first record (like the example above). The struct_address_str[0][0] returns the first element of the row. After I make the first transformation, the struct_address_str[0][0] no more returns the first element but first character of the element.



However, after the transformation, if I do tuple[0][0], I get back A while I want to the output to be ABC.



How do I get this working?



Following is the code that I am using:



    cursor = conn.cursor();

### Structure Address Data ###
cursor.execute("SELECT id,... FROM ...");

#converted the cursor to list
struct_address = list(cursor.fetchall())


#converted all the list elements to string
struct_address_str = [str(i) for i in struct_address]

#Checking the values
print(struct_address_str[0][1], sep="n")
print(struct_address_str[0][2], sep="n")
print(struct_address_str[0], sep="n")

#converted all the list elements to uppercase
struct_address_upper = [i.upper() for i in struct_address_str]

#removing all the special characters
#cli_add_no_sp_char = [s.translate(str.maketrans('', '', ''(#),-".')) for s in cli_address_upper]
struct_add_no_sp_char = [s.translate(str.maketrans(''(#),-"./', ' ', '')) for s in struct_address_upper]









share|improve this question

























  • The line struct_address_str = [str(i) for i in struct_address] seems to be doing exactly what you say you want to do. What exactly is the problem with it?

    – jasonharper
    Nov 20 '18 at 17:08











  • Like I mentioned above, I intend to get ABC as output of print(struct_address_str[0][1]), instead of A.

    – Sushant Vasishta
    Nov 20 '18 at 17:11











  • That's a completely unreasonable intention. struct_address_str[0] is the entire first element of the list, if that's a string then adding [1] can only be a single character from that string.

    – jasonharper
    Nov 20 '18 at 17:17











  • The cursor returns multiple records. The struct_address_str[0] returns the first record (like the example above). The struct_address_str[0][0] returns the first element of the row. After I make the first transformation, the struct_address_str[0][0] no more returns the first element but first character of the element. And looks like you downvoted without even understanding the question. Thanks. I take that as my fault too since I probably didn't elaborate well enough.

    – Sushant Vasishta
    Nov 20 '18 at 17:19


















-1















I created a tuple by reading data from a MySQL table. All the elements are of mixed data types and to be able to apply few string operations (upper case, removal of special characters etc), I need to convert all those elements to string.



I tried "".str() and .join() but the resultant is a pure string and I lose information about individual elements.



Something like:



(ABC, XYZ, 234, QWE, 578)   <-- mixed datatypes but I can do tuple[0] to just fetch ABC


The cursor returns multiple records. The struct_address_str[0] returns the first record (like the example above). The struct_address_str[0][0] returns the first element of the row. After I make the first transformation, the struct_address_str[0][0] no more returns the first element but first character of the element.



However, after the transformation, if I do tuple[0][0], I get back A while I want to the output to be ABC.



How do I get this working?



Following is the code that I am using:



    cursor = conn.cursor();

### Structure Address Data ###
cursor.execute("SELECT id,... FROM ...");

#converted the cursor to list
struct_address = list(cursor.fetchall())


#converted all the list elements to string
struct_address_str = [str(i) for i in struct_address]

#Checking the values
print(struct_address_str[0][1], sep="n")
print(struct_address_str[0][2], sep="n")
print(struct_address_str[0], sep="n")

#converted all the list elements to uppercase
struct_address_upper = [i.upper() for i in struct_address_str]

#removing all the special characters
#cli_add_no_sp_char = [s.translate(str.maketrans('', '', ''(#),-".')) for s in cli_address_upper]
struct_add_no_sp_char = [s.translate(str.maketrans(''(#),-"./', ' ', '')) for s in struct_address_upper]









share|improve this question

























  • The line struct_address_str = [str(i) for i in struct_address] seems to be doing exactly what you say you want to do. What exactly is the problem with it?

    – jasonharper
    Nov 20 '18 at 17:08











  • Like I mentioned above, I intend to get ABC as output of print(struct_address_str[0][1]), instead of A.

    – Sushant Vasishta
    Nov 20 '18 at 17:11











  • That's a completely unreasonable intention. struct_address_str[0] is the entire first element of the list, if that's a string then adding [1] can only be a single character from that string.

    – jasonharper
    Nov 20 '18 at 17:17











  • The cursor returns multiple records. The struct_address_str[0] returns the first record (like the example above). The struct_address_str[0][0] returns the first element of the row. After I make the first transformation, the struct_address_str[0][0] no more returns the first element but first character of the element. And looks like you downvoted without even understanding the question. Thanks. I take that as my fault too since I probably didn't elaborate well enough.

    – Sushant Vasishta
    Nov 20 '18 at 17:19
















-1












-1








-1








I created a tuple by reading data from a MySQL table. All the elements are of mixed data types and to be able to apply few string operations (upper case, removal of special characters etc), I need to convert all those elements to string.



I tried "".str() and .join() but the resultant is a pure string and I lose information about individual elements.



Something like:



(ABC, XYZ, 234, QWE, 578)   <-- mixed datatypes but I can do tuple[0] to just fetch ABC


The cursor returns multiple records. The struct_address_str[0] returns the first record (like the example above). The struct_address_str[0][0] returns the first element of the row. After I make the first transformation, the struct_address_str[0][0] no more returns the first element but first character of the element.



However, after the transformation, if I do tuple[0][0], I get back A while I want to the output to be ABC.



How do I get this working?



Following is the code that I am using:



    cursor = conn.cursor();

### Structure Address Data ###
cursor.execute("SELECT id,... FROM ...");

#converted the cursor to list
struct_address = list(cursor.fetchall())


#converted all the list elements to string
struct_address_str = [str(i) for i in struct_address]

#Checking the values
print(struct_address_str[0][1], sep="n")
print(struct_address_str[0][2], sep="n")
print(struct_address_str[0], sep="n")

#converted all the list elements to uppercase
struct_address_upper = [i.upper() for i in struct_address_str]

#removing all the special characters
#cli_add_no_sp_char = [s.translate(str.maketrans('', '', ''(#),-".')) for s in cli_address_upper]
struct_add_no_sp_char = [s.translate(str.maketrans(''(#),-"./', ' ', '')) for s in struct_address_upper]









share|improve this question
















I created a tuple by reading data from a MySQL table. All the elements are of mixed data types and to be able to apply few string operations (upper case, removal of special characters etc), I need to convert all those elements to string.



I tried "".str() and .join() but the resultant is a pure string and I lose information about individual elements.



Something like:



(ABC, XYZ, 234, QWE, 578)   <-- mixed datatypes but I can do tuple[0] to just fetch ABC


The cursor returns multiple records. The struct_address_str[0] returns the first record (like the example above). The struct_address_str[0][0] returns the first element of the row. After I make the first transformation, the struct_address_str[0][0] no more returns the first element but first character of the element.



However, after the transformation, if I do tuple[0][0], I get back A while I want to the output to be ABC.



How do I get this working?



Following is the code that I am using:



    cursor = conn.cursor();

### Structure Address Data ###
cursor.execute("SELECT id,... FROM ...");

#converted the cursor to list
struct_address = list(cursor.fetchall())


#converted all the list elements to string
struct_address_str = [str(i) for i in struct_address]

#Checking the values
print(struct_address_str[0][1], sep="n")
print(struct_address_str[0][2], sep="n")
print(struct_address_str[0], sep="n")

#converted all the list elements to uppercase
struct_address_upper = [i.upper() for i in struct_address_str]

#removing all the special characters
#cli_add_no_sp_char = [s.translate(str.maketrans('', '', ''(#),-".')) for s in cli_address_upper]
struct_add_no_sp_char = [s.translate(str.maketrans(''(#),-"./', ' ', '')) for s in struct_address_upper]






python python-3.x






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 17:44







Sushant Vasishta

















asked Nov 20 '18 at 17:04









Sushant VasishtaSushant Vasishta

948




948













  • The line struct_address_str = [str(i) for i in struct_address] seems to be doing exactly what you say you want to do. What exactly is the problem with it?

    – jasonharper
    Nov 20 '18 at 17:08











  • Like I mentioned above, I intend to get ABC as output of print(struct_address_str[0][1]), instead of A.

    – Sushant Vasishta
    Nov 20 '18 at 17:11











  • That's a completely unreasonable intention. struct_address_str[0] is the entire first element of the list, if that's a string then adding [1] can only be a single character from that string.

    – jasonharper
    Nov 20 '18 at 17:17











  • The cursor returns multiple records. The struct_address_str[0] returns the first record (like the example above). The struct_address_str[0][0] returns the first element of the row. After I make the first transformation, the struct_address_str[0][0] no more returns the first element but first character of the element. And looks like you downvoted without even understanding the question. Thanks. I take that as my fault too since I probably didn't elaborate well enough.

    – Sushant Vasishta
    Nov 20 '18 at 17:19





















  • The line struct_address_str = [str(i) for i in struct_address] seems to be doing exactly what you say you want to do. What exactly is the problem with it?

    – jasonharper
    Nov 20 '18 at 17:08











  • Like I mentioned above, I intend to get ABC as output of print(struct_address_str[0][1]), instead of A.

    – Sushant Vasishta
    Nov 20 '18 at 17:11











  • That's a completely unreasonable intention. struct_address_str[0] is the entire first element of the list, if that's a string then adding [1] can only be a single character from that string.

    – jasonharper
    Nov 20 '18 at 17:17











  • The cursor returns multiple records. The struct_address_str[0] returns the first record (like the example above). The struct_address_str[0][0] returns the first element of the row. After I make the first transformation, the struct_address_str[0][0] no more returns the first element but first character of the element. And looks like you downvoted without even understanding the question. Thanks. I take that as my fault too since I probably didn't elaborate well enough.

    – Sushant Vasishta
    Nov 20 '18 at 17:19



















The line struct_address_str = [str(i) for i in struct_address] seems to be doing exactly what you say you want to do. What exactly is the problem with it?

– jasonharper
Nov 20 '18 at 17:08





The line struct_address_str = [str(i) for i in struct_address] seems to be doing exactly what you say you want to do. What exactly is the problem with it?

– jasonharper
Nov 20 '18 at 17:08













Like I mentioned above, I intend to get ABC as output of print(struct_address_str[0][1]), instead of A.

– Sushant Vasishta
Nov 20 '18 at 17:11





Like I mentioned above, I intend to get ABC as output of print(struct_address_str[0][1]), instead of A.

– Sushant Vasishta
Nov 20 '18 at 17:11













That's a completely unreasonable intention. struct_address_str[0] is the entire first element of the list, if that's a string then adding [1] can only be a single character from that string.

– jasonharper
Nov 20 '18 at 17:17





That's a completely unreasonable intention. struct_address_str[0] is the entire first element of the list, if that's a string then adding [1] can only be a single character from that string.

– jasonharper
Nov 20 '18 at 17:17













The cursor returns multiple records. The struct_address_str[0] returns the first record (like the example above). The struct_address_str[0][0] returns the first element of the row. After I make the first transformation, the struct_address_str[0][0] no more returns the first element but first character of the element. And looks like you downvoted without even understanding the question. Thanks. I take that as my fault too since I probably didn't elaborate well enough.

– Sushant Vasishta
Nov 20 '18 at 17:19







The cursor returns multiple records. The struct_address_str[0] returns the first record (like the example above). The struct_address_str[0][0] returns the first element of the row. After I make the first transformation, the struct_address_str[0][0] no more returns the first element but first character of the element. And looks like you downvoted without even understanding the question. Thanks. I take that as my fault too since I probably didn't elaborate well enough.

– Sushant Vasishta
Nov 20 '18 at 17:19














1 Answer
1






active

oldest

votes


















1














What about :



struct_address_str = [[str(i) for i in x] for x in struct_address]


And then again:



struct_address_upper = [[i.upper() for i in x] for x in struct_address_str]


Of course you could combine the two in one line by using "str(i).upper()". I would probably define a function sanitize(i) making all needed operations and then use it in the list comprehension.






share|improve this answer


























  • Thanks @Rolvernew that game me an idea. I was able to use that construct.

    – Sushant Vasishta
    Nov 20 '18 at 18:59











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%2f53398006%2fhow-to-convert-the-tuple-elements-to-string-without-losing-the-tuple-properties%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














What about :



struct_address_str = [[str(i) for i in x] for x in struct_address]


And then again:



struct_address_upper = [[i.upper() for i in x] for x in struct_address_str]


Of course you could combine the two in one line by using "str(i).upper()". I would probably define a function sanitize(i) making all needed operations and then use it in the list comprehension.






share|improve this answer


























  • Thanks @Rolvernew that game me an idea. I was able to use that construct.

    – Sushant Vasishta
    Nov 20 '18 at 18:59
















1














What about :



struct_address_str = [[str(i) for i in x] for x in struct_address]


And then again:



struct_address_upper = [[i.upper() for i in x] for x in struct_address_str]


Of course you could combine the two in one line by using "str(i).upper()". I would probably define a function sanitize(i) making all needed operations and then use it in the list comprehension.






share|improve this answer


























  • Thanks @Rolvernew that game me an idea. I was able to use that construct.

    – Sushant Vasishta
    Nov 20 '18 at 18:59














1












1








1







What about :



struct_address_str = [[str(i) for i in x] for x in struct_address]


And then again:



struct_address_upper = [[i.upper() for i in x] for x in struct_address_str]


Of course you could combine the two in one line by using "str(i).upper()". I would probably define a function sanitize(i) making all needed operations and then use it in the list comprehension.






share|improve this answer















What about :



struct_address_str = [[str(i) for i in x] for x in struct_address]


And then again:



struct_address_upper = [[i.upper() for i in x] for x in struct_address_str]


Of course you could combine the two in one line by using "str(i).upper()". I would probably define a function sanitize(i) making all needed operations and then use it in the list comprehension.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 '18 at 17:56

























answered Nov 20 '18 at 17:50









RolvernewRolvernew

1817




1817













  • Thanks @Rolvernew that game me an idea. I was able to use that construct.

    – Sushant Vasishta
    Nov 20 '18 at 18:59



















  • Thanks @Rolvernew that game me an idea. I was able to use that construct.

    – Sushant Vasishta
    Nov 20 '18 at 18:59

















Thanks @Rolvernew that game me an idea. I was able to use that construct.

– Sushant Vasishta
Nov 20 '18 at 18:59





Thanks @Rolvernew that game me an idea. I was able to use that construct.

– Sushant Vasishta
Nov 20 '18 at 18:59


















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%2f53398006%2fhow-to-convert-the-tuple-elements-to-string-without-losing-the-tuple-properties%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

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

Npm cannot find a required file even through it is in the searched directory