How to append string in the middle of a pre-existing csv line?
Question:
I am fairly new to working with python and finally encountered a problem I cannot circumvent. I will make this fairly simple.
I have a csv file with many lines that looks like this once I create a list variable:
['1t10000t11000tabcdeft1t+t10000t11000t"0,0,0"t1t1000t0n']
I want to add 2 new string variables after the final t0 before the n. Its important to indicate I still want the t before str1 and str2. So the output I desire should look like this:
['1t10000t11000tabcdeft1t+t10000t11000t"0,0,0"t1t1000t0tstr1tstr2n']
Thanks for your help!
str1 = hello
str2 = world
line = ['1t10000t11000tabcdeft1t+t10000t11000t"0,0,0"t1t1000t0n']
line.append(('t') + str1 + ('t') + str2)
print(line)
Current output:
['1t10000t11000tabcdeft1t+t10000t11000t"0,0,0"t1t1000t0n', 'tstr1tstr2']
Answers:
append()
is for adding an element to a list. To add to a string, use +=
on the variable that contains the string, which is line[0]
.
line[0] += f't{str1}t{str2}'
That said, it’s strange that you have the entire line as a single element of the list, rather than parsing the CSV row into a list of fields, using t
as the field delimiter.
The append method adds the input to the list you use it on.
Since line is a list, you are getting a new entry into your list.
If you want to keep your string inside the list, you need to access the 0th index of the list to change the string:
line[0]
Once you have the string you need to separate it into two and add your new strings:
line[0] = line[0][:-2] + newinput + line[0][-2:]
Note the -2 separated because you want to separate the last two characters of your string: n
I am fairly new to working with python and finally encountered a problem I cannot circumvent. I will make this fairly simple.
I have a csv file with many lines that looks like this once I create a list variable:
['1t10000t11000tabcdeft1t+t10000t11000t"0,0,0"t1t1000t0n']
I want to add 2 new string variables after the final t0 before the n. Its important to indicate I still want the t before str1 and str2. So the output I desire should look like this:
['1t10000t11000tabcdeft1t+t10000t11000t"0,0,0"t1t1000t0tstr1tstr2n']
Thanks for your help!
str1 = hello
str2 = world
line = ['1t10000t11000tabcdeft1t+t10000t11000t"0,0,0"t1t1000t0n']
line.append(('t') + str1 + ('t') + str2)
print(line)
Current output:
['1t10000t11000tabcdeft1t+t10000t11000t"0,0,0"t1t1000t0n', 'tstr1tstr2']
append()
is for adding an element to a list. To add to a string, use +=
on the variable that contains the string, which is line[0]
.
line[0] += f't{str1}t{str2}'
That said, it’s strange that you have the entire line as a single element of the list, rather than parsing the CSV row into a list of fields, using t
as the field delimiter.
The append method adds the input to the list you use it on.
Since line is a list, you are getting a new entry into your list.
If you want to keep your string inside the list, you need to access the 0th index of the list to change the string:
line[0]
Once you have the string you need to separate it into two and add your new strings:
line[0] = line[0][:-2] + newinput + line[0][-2:]
Note the -2 separated because you want to separate the last two characters of your string: n