Reputation: 47
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]
Upvotes: 0
Views: 111
Reputation: 4547
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])
Upvotes: 1
Reputation: 837
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
Upvotes: 0