Python Forum
insert value to specific line in CSV file - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: insert value to specific line in CSV file (/thread-21928.html)



insert value to specific line in CSV file - asheru93 - Oct-21-2019

Hello,

I have a CSV.file that looks something like this:

Carrot
Cake
Other,Values
test1,test
Abc,test,test1
MyString
The CSV values might change.
In case, IF "MyString" is in the CSV file, I want to add two new values('myValue1','myValue2') between first two, so between 'Carrot' and 'Cake' in this case. Then the CSV file will be:

Carrot
myValue1
myValue2
Cake
Other,Values
test1,test
Abc,test,test1
MyString
If "myString" is not in the CSV file then the original file remains the same.


RE: insert value to specific line in CSV file - Larz60+ - Oct-21-2019

You can use any text editor to do this