Can't figure out why this assignment code throws an error










0















NOTE: I have googled this and have read the many posts about the same thing but I'm just not understanding the explanations. I know the answer that string variables are immutable, but I can't understand how I can get the values and split them into separate variables without changing the contents of the variables I'm putting the data in. If someone could show me how to write this code in a legal way I'd greatly appreciate it.



I'm new to python with experience in a couple other languages. Code is at the end of the post.



The file input.txt contains several lines like:



50,100



20,110



30,70



I am trying to open the file, read the lines into a list variable, then split each value into separate variables at the comma.



I get the error:




TypeError: 'str' object does not support item assignment



at the line that reads: degrees[i] = current[0]




I have spent a couple hours now reading posts about how strings are immutable but I'm just not understanding why this shouldn't work (or how to write it so it does work). The variable degrees starts empty. I don't know how else to create it in a way that I'm not changing anything or how to do this differently. I tried casting the values from current as int and putting them in the degrees variable and still get the TypeError.



I also don't understand why I need the line degrees = "" to begin with. My understanding of python is you don't have to declare variables, you just make up a name and use it when you need it. But if I leave out the line degrees = "" I get an error saying




NameError: name 'degrees' is not defined




but that would seem to mean that I somehow have to declare the variable before using it.



Thanks for any help.



f = open('input.txt')

content_list = f.readlines()

f.close()

degrees=""

volume=""

for i in range(len(content_list)):

content_list[i] = content_list[i].strip('n')

current = content_list[i].split(",")

print(content_list[i])

degrees[i] = current[0]

volume[i] = current[1]









share|improve this question




























    0















    NOTE: I have googled this and have read the many posts about the same thing but I'm just not understanding the explanations. I know the answer that string variables are immutable, but I can't understand how I can get the values and split them into separate variables without changing the contents of the variables I'm putting the data in. If someone could show me how to write this code in a legal way I'd greatly appreciate it.



    I'm new to python with experience in a couple other languages. Code is at the end of the post.



    The file input.txt contains several lines like:



    50,100



    20,110



    30,70



    I am trying to open the file, read the lines into a list variable, then split each value into separate variables at the comma.



    I get the error:




    TypeError: 'str' object does not support item assignment



    at the line that reads: degrees[i] = current[0]




    I have spent a couple hours now reading posts about how strings are immutable but I'm just not understanding why this shouldn't work (or how to write it so it does work). The variable degrees starts empty. I don't know how else to create it in a way that I'm not changing anything or how to do this differently. I tried casting the values from current as int and putting them in the degrees variable and still get the TypeError.



    I also don't understand why I need the line degrees = "" to begin with. My understanding of python is you don't have to declare variables, you just make up a name and use it when you need it. But if I leave out the line degrees = "" I get an error saying




    NameError: name 'degrees' is not defined




    but that would seem to mean that I somehow have to declare the variable before using it.



    Thanks for any help.



    f = open('input.txt')

    content_list = f.readlines()

    f.close()

    degrees=""

    volume=""

    for i in range(len(content_list)):

    content_list[i] = content_list[i].strip('n')

    current = content_list[i].split(",")

    print(content_list[i])

    degrees[i] = current[0]

    volume[i] = current[1]









    share|improve this question


























      0












      0








      0








      NOTE: I have googled this and have read the many posts about the same thing but I'm just not understanding the explanations. I know the answer that string variables are immutable, but I can't understand how I can get the values and split them into separate variables without changing the contents of the variables I'm putting the data in. If someone could show me how to write this code in a legal way I'd greatly appreciate it.



      I'm new to python with experience in a couple other languages. Code is at the end of the post.



      The file input.txt contains several lines like:



      50,100



      20,110



      30,70



      I am trying to open the file, read the lines into a list variable, then split each value into separate variables at the comma.



      I get the error:




      TypeError: 'str' object does not support item assignment



      at the line that reads: degrees[i] = current[0]




      I have spent a couple hours now reading posts about how strings are immutable but I'm just not understanding why this shouldn't work (or how to write it so it does work). The variable degrees starts empty. I don't know how else to create it in a way that I'm not changing anything or how to do this differently. I tried casting the values from current as int and putting them in the degrees variable and still get the TypeError.



      I also don't understand why I need the line degrees = "" to begin with. My understanding of python is you don't have to declare variables, you just make up a name and use it when you need it. But if I leave out the line degrees = "" I get an error saying




      NameError: name 'degrees' is not defined




      but that would seem to mean that I somehow have to declare the variable before using it.



      Thanks for any help.



      f = open('input.txt')

      content_list = f.readlines()

      f.close()

      degrees=""

      volume=""

      for i in range(len(content_list)):

      content_list[i] = content_list[i].strip('n')

      current = content_list[i].split(",")

      print(content_list[i])

      degrees[i] = current[0]

      volume[i] = current[1]









      share|improve this question
















      NOTE: I have googled this and have read the many posts about the same thing but I'm just not understanding the explanations. I know the answer that string variables are immutable, but I can't understand how I can get the values and split them into separate variables without changing the contents of the variables I'm putting the data in. If someone could show me how to write this code in a legal way I'd greatly appreciate it.



      I'm new to python with experience in a couple other languages. Code is at the end of the post.



      The file input.txt contains several lines like:



      50,100



      20,110



      30,70



      I am trying to open the file, read the lines into a list variable, then split each value into separate variables at the comma.



      I get the error:




      TypeError: 'str' object does not support item assignment



      at the line that reads: degrees[i] = current[0]




      I have spent a couple hours now reading posts about how strings are immutable but I'm just not understanding why this shouldn't work (or how to write it so it does work). The variable degrees starts empty. I don't know how else to create it in a way that I'm not changing anything or how to do this differently. I tried casting the values from current as int and putting them in the degrees variable and still get the TypeError.



      I also don't understand why I need the line degrees = "" to begin with. My understanding of python is you don't have to declare variables, you just make up a name and use it when you need it. But if I leave out the line degrees = "" I get an error saying




      NameError: name 'degrees' is not defined




      but that would seem to mean that I somehow have to declare the variable before using it.



      Thanks for any help.



      f = open('input.txt')

      content_list = f.readlines()

      f.close()

      degrees=""

      volume=""

      for i in range(len(content_list)):

      content_list[i] = content_list[i].strip('n')

      current = content_list[i].split(",")

      print(content_list[i])

      degrees[i] = current[0]

      volume[i] = current[1]






      python python-3.x






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 5:22









      Aqueous Carlos

      371415




      371415










      asked Nov 13 '18 at 3:41









      Eric OswaldEric Oswald

      175




      175






















          2 Answers
          2






          active

          oldest

          votes


















          1














          My understanding is that you want to store each column of your file in a different variable. In Python, you can store data using many different variable types, but they do not all work the same way. As the TypeError points out, you cannot use item assignment with string-type variables. However you can perform operations on strings such as:



          degrees = ''
          string1 = 'abc'
          degrees += string1


          And then you could use a separator in between the strings you store such as a slash, so that you are able to identify the beginning and end of each string.



          string2 = 'def'
          degrees += '/' + string2


          Assigning the value '' to degrees simply means that degrees is a variable of type string which contains nothing. Furthermore it defines the variable degrees, so that if you call it later on, the interpreter knows what you are referring to.



          However it is way more convenient to use variable types which support item assignment, such as lists. You can then use the append method to store new values at each iteration.



          f = open('input.txt')
          content_list = f.readlines()
          f.close()
          degrees =
          volume =
          for i in range(len(content_list)):
          content_list[i] = content_list[i].strip('n')
          current = content_list[i].split(",")
          degrees.append(current[0])
          volume.append(current[1])





          share|improve this answer























          • Thank you so much, this is exactly what I needed. Coming from other c type languages this immutable stuff isn't making sense, but I see now how I can get it to understand that I'm trying to add the values onto a list. And thanks for explaining and showing this. I posted this in a few places and just got back "strings are immutable" which is what I started by saying I had already read and didn't understand.

            – Eric Oswald
            Nov 13 '18 at 4:34












          • No worries, it is sometimes very misleading when one comes to a new language and tries to apply logic from another one. I have to admit I hated Python for this at first, but I am definitely not coming back now. I hope it will be the same for you. Oh, and feel free to accept the answer as well.

            – Patol75
            Nov 19 '18 at 8:52


















          0















          Python strings are immutable.




          This simply means that once you create a string in python, you can not change it.



          a = "Hello"
          a[0] = 'J' # This is not allowed by python because you are updating contents of a string


          I can see how this can be frustrating coming from other languages where this is supported. You can take a look at bytearrays in python which are mutable






          share|improve this answer






















            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%2f53273449%2fcant-figure-out-why-this-assignment-code-throws-an-error%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









            1














            My understanding is that you want to store each column of your file in a different variable. In Python, you can store data using many different variable types, but they do not all work the same way. As the TypeError points out, you cannot use item assignment with string-type variables. However you can perform operations on strings such as:



            degrees = ''
            string1 = 'abc'
            degrees += string1


            And then you could use a separator in between the strings you store such as a slash, so that you are able to identify the beginning and end of each string.



            string2 = 'def'
            degrees += '/' + string2


            Assigning the value '' to degrees simply means that degrees is a variable of type string which contains nothing. Furthermore it defines the variable degrees, so that if you call it later on, the interpreter knows what you are referring to.



            However it is way more convenient to use variable types which support item assignment, such as lists. You can then use the append method to store new values at each iteration.



            f = open('input.txt')
            content_list = f.readlines()
            f.close()
            degrees =
            volume =
            for i in range(len(content_list)):
            content_list[i] = content_list[i].strip('n')
            current = content_list[i].split(",")
            degrees.append(current[0])
            volume.append(current[1])





            share|improve this answer























            • Thank you so much, this is exactly what I needed. Coming from other c type languages this immutable stuff isn't making sense, but I see now how I can get it to understand that I'm trying to add the values onto a list. And thanks for explaining and showing this. I posted this in a few places and just got back "strings are immutable" which is what I started by saying I had already read and didn't understand.

              – Eric Oswald
              Nov 13 '18 at 4:34












            • No worries, it is sometimes very misleading when one comes to a new language and tries to apply logic from another one. I have to admit I hated Python for this at first, but I am definitely not coming back now. I hope it will be the same for you. Oh, and feel free to accept the answer as well.

              – Patol75
              Nov 19 '18 at 8:52















            1














            My understanding is that you want to store each column of your file in a different variable. In Python, you can store data using many different variable types, but they do not all work the same way. As the TypeError points out, you cannot use item assignment with string-type variables. However you can perform operations on strings such as:



            degrees = ''
            string1 = 'abc'
            degrees += string1


            And then you could use a separator in between the strings you store such as a slash, so that you are able to identify the beginning and end of each string.



            string2 = 'def'
            degrees += '/' + string2


            Assigning the value '' to degrees simply means that degrees is a variable of type string which contains nothing. Furthermore it defines the variable degrees, so that if you call it later on, the interpreter knows what you are referring to.



            However it is way more convenient to use variable types which support item assignment, such as lists. You can then use the append method to store new values at each iteration.



            f = open('input.txt')
            content_list = f.readlines()
            f.close()
            degrees =
            volume =
            for i in range(len(content_list)):
            content_list[i] = content_list[i].strip('n')
            current = content_list[i].split(",")
            degrees.append(current[0])
            volume.append(current[1])





            share|improve this answer























            • Thank you so much, this is exactly what I needed. Coming from other c type languages this immutable stuff isn't making sense, but I see now how I can get it to understand that I'm trying to add the values onto a list. And thanks for explaining and showing this. I posted this in a few places and just got back "strings are immutable" which is what I started by saying I had already read and didn't understand.

              – Eric Oswald
              Nov 13 '18 at 4:34












            • No worries, it is sometimes very misleading when one comes to a new language and tries to apply logic from another one. I have to admit I hated Python for this at first, but I am definitely not coming back now. I hope it will be the same for you. Oh, and feel free to accept the answer as well.

              – Patol75
              Nov 19 '18 at 8:52













            1












            1








            1







            My understanding is that you want to store each column of your file in a different variable. In Python, you can store data using many different variable types, but they do not all work the same way. As the TypeError points out, you cannot use item assignment with string-type variables. However you can perform operations on strings such as:



            degrees = ''
            string1 = 'abc'
            degrees += string1


            And then you could use a separator in between the strings you store such as a slash, so that you are able to identify the beginning and end of each string.



            string2 = 'def'
            degrees += '/' + string2


            Assigning the value '' to degrees simply means that degrees is a variable of type string which contains nothing. Furthermore it defines the variable degrees, so that if you call it later on, the interpreter knows what you are referring to.



            However it is way more convenient to use variable types which support item assignment, such as lists. You can then use the append method to store new values at each iteration.



            f = open('input.txt')
            content_list = f.readlines()
            f.close()
            degrees =
            volume =
            for i in range(len(content_list)):
            content_list[i] = content_list[i].strip('n')
            current = content_list[i].split(",")
            degrees.append(current[0])
            volume.append(current[1])





            share|improve this answer













            My understanding is that you want to store each column of your file in a different variable. In Python, you can store data using many different variable types, but they do not all work the same way. As the TypeError points out, you cannot use item assignment with string-type variables. However you can perform operations on strings such as:



            degrees = ''
            string1 = 'abc'
            degrees += string1


            And then you could use a separator in between the strings you store such as a slash, so that you are able to identify the beginning and end of each string.



            string2 = 'def'
            degrees += '/' + string2


            Assigning the value '' to degrees simply means that degrees is a variable of type string which contains nothing. Furthermore it defines the variable degrees, so that if you call it later on, the interpreter knows what you are referring to.



            However it is way more convenient to use variable types which support item assignment, such as lists. You can then use the append method to store new values at each iteration.



            f = open('input.txt')
            content_list = f.readlines()
            f.close()
            degrees =
            volume =
            for i in range(len(content_list)):
            content_list[i] = content_list[i].strip('n')
            current = content_list[i].split(",")
            degrees.append(current[0])
            volume.append(current[1])






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 13 '18 at 4:03









            Patol75Patol75

            6236




            6236












            • Thank you so much, this is exactly what I needed. Coming from other c type languages this immutable stuff isn't making sense, but I see now how I can get it to understand that I'm trying to add the values onto a list. And thanks for explaining and showing this. I posted this in a few places and just got back "strings are immutable" which is what I started by saying I had already read and didn't understand.

              – Eric Oswald
              Nov 13 '18 at 4:34












            • No worries, it is sometimes very misleading when one comes to a new language and tries to apply logic from another one. I have to admit I hated Python for this at first, but I am definitely not coming back now. I hope it will be the same for you. Oh, and feel free to accept the answer as well.

              – Patol75
              Nov 19 '18 at 8:52

















            • Thank you so much, this is exactly what I needed. Coming from other c type languages this immutable stuff isn't making sense, but I see now how I can get it to understand that I'm trying to add the values onto a list. And thanks for explaining and showing this. I posted this in a few places and just got back "strings are immutable" which is what I started by saying I had already read and didn't understand.

              – Eric Oswald
              Nov 13 '18 at 4:34












            • No worries, it is sometimes very misleading when one comes to a new language and tries to apply logic from another one. I have to admit I hated Python for this at first, but I am definitely not coming back now. I hope it will be the same for you. Oh, and feel free to accept the answer as well.

              – Patol75
              Nov 19 '18 at 8:52
















            Thank you so much, this is exactly what I needed. Coming from other c type languages this immutable stuff isn't making sense, but I see now how I can get it to understand that I'm trying to add the values onto a list. And thanks for explaining and showing this. I posted this in a few places and just got back "strings are immutable" which is what I started by saying I had already read and didn't understand.

            – Eric Oswald
            Nov 13 '18 at 4:34






            Thank you so much, this is exactly what I needed. Coming from other c type languages this immutable stuff isn't making sense, but I see now how I can get it to understand that I'm trying to add the values onto a list. And thanks for explaining and showing this. I posted this in a few places and just got back "strings are immutable" which is what I started by saying I had already read and didn't understand.

            – Eric Oswald
            Nov 13 '18 at 4:34














            No worries, it is sometimes very misleading when one comes to a new language and tries to apply logic from another one. I have to admit I hated Python for this at first, but I am definitely not coming back now. I hope it will be the same for you. Oh, and feel free to accept the answer as well.

            – Patol75
            Nov 19 '18 at 8:52





            No worries, it is sometimes very misleading when one comes to a new language and tries to apply logic from another one. I have to admit I hated Python for this at first, but I am definitely not coming back now. I hope it will be the same for you. Oh, and feel free to accept the answer as well.

            – Patol75
            Nov 19 '18 at 8:52













            0















            Python strings are immutable.




            This simply means that once you create a string in python, you can not change it.



            a = "Hello"
            a[0] = 'J' # This is not allowed by python because you are updating contents of a string


            I can see how this can be frustrating coming from other languages where this is supported. You can take a look at bytearrays in python which are mutable






            share|improve this answer



























              0















              Python strings are immutable.




              This simply means that once you create a string in python, you can not change it.



              a = "Hello"
              a[0] = 'J' # This is not allowed by python because you are updating contents of a string


              I can see how this can be frustrating coming from other languages where this is supported. You can take a look at bytearrays in python which are mutable






              share|improve this answer

























                0












                0








                0








                Python strings are immutable.




                This simply means that once you create a string in python, you can not change it.



                a = "Hello"
                a[0] = 'J' # This is not allowed by python because you are updating contents of a string


                I can see how this can be frustrating coming from other languages where this is supported. You can take a look at bytearrays in python which are mutable






                share|improve this answer














                Python strings are immutable.




                This simply means that once you create a string in python, you can not change it.



                a = "Hello"
                a[0] = 'J' # This is not allowed by python because you are updating contents of a string


                I can see how this can be frustrating coming from other languages where this is supported. You can take a look at bytearrays in python which are mutable







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 13 '18 at 3:48









                BishalBishal

                556216




                556216



























                    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%2f53273449%2fcant-figure-out-why-this-assignment-code-throws-an-error%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

                    𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

                    Crossroads (UK TV series)

                    ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế