Mar-26-2022, 09:04 AM
I am new in Python and I am stuck with one problem for a few days now. I made a script that:
-takes data from CSV file
-sort it by same values in first column of data file
-instert sorted data in specifield line in different template text file
-save the file in as many copies as there are different values in first column from data file This picture below show how it works:
![[Image: V0hAX.png]](https://i.stack.imgur.com/V0hAX.png)
But there are two more things I need to do. When in separate files as showed above, there are some of the same values from second column of the data file, then this file should insert value from third column instead of repeating the same value from second column. On the picture below I showed how it should look like
![[Image: cw7oK.png]](https://i.stack.imgur.com/cw7oK.png)
What I also need is to add somewhere separeted value of first column from data file by "_".
There is datafile:
-takes data from CSV file
-sort it by same values in first column of data file
-instert sorted data in specifield line in different template text file
-save the file in as many copies as there are different values in first column from data file This picture below show how it works:
![[Image: V0hAX.png]](https://i.stack.imgur.com/V0hAX.png)
But there are two more things I need to do. When in separate files as showed above, there are some of the same values from second column of the data file, then this file should insert value from third column instead of repeating the same value from second column. On the picture below I showed how it should look like
![[Image: cw7oK.png]](https://i.stack.imgur.com/cw7oK.png)
What I also need is to add somewhere separeted value of first column from data file by "_".
There is datafile:
111_0,3005,QWE 111_0,3006,SDE 111_0,3006,LFR 111_1,3005,QWE 111_1,5345,JTR 112_0,3103,JPP 112_0,3343,PDK 113_0,2137,TRE 113_0,2137,OMGand there is code i made:
import shutil with open("data.csv") as f: contents = f.read() contents = contents.splitlines() values_per_baseline = dict() for line in contents: key = line.split(',')[0] values = line.split(',')[1:] if key not in values_per_baseline: values_per_baseline[key] = [] values_per_baseline[key].append(values) for file in values_per_baseline.keys(): x = 3 shutil.copyfile("of.txt", (f"of_%s.txt" % file)) filename = f"of_%s.txt" % file for values in values_per_baseline[file]: with open(filename, "r") as f: contents = f.readlines() contents.insert(x, ' o = ' + values[0] + '\n ' + 'a = ' + values[1] +'\n') with open(filename, "w") as f: contents = "".join(contents) f.write(contents) f.close()Any suggestions would be appreciated.