Python Forum

Full Version: insert value to specific line in CSV file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
You can use any text editor to do this