Есть одно из возможных решений:
#Open the fist file
mfile = open('file.csv', 'r')
string = mfile.read()
mfile.close()
# Split on the line breaks
string = string.split("\n")
#CAUTION if you CSV file uses ";" instead "," change it on the code!
condition = ''
newString = []
for i in range(len(string)):
# Check if condition is trully oneline
if(len(string[i].split(',')) ==1):
condition = string[i]
#Change the string 'header1,header2 to you header
elif (string[i] == 'header1,header2'):
pass
else:
newString.append(string[i] + ","+condition)
mfile = open('outfile.csv', 'w')
mfile.write('header1,header2\n')
for i in newString:
mfile.write(i + '\n')
Я использовал это как содержимое file.csv (ввод):
condidtion1
header1,header2
2,3
2,3
2,3
2,3
condidtion2
header1,header2
3,4
3,4
3,4
3,4
3,4
3,4
После запуска кода, файл outfile.csv выглядит так (вывод):
header1,header2
2,3,condidtion1
2,3,condidtion1
2,3,condidtion1
2,3,condidtion1
3,4,condidtion2
3,4,condidtion2
3,4,condidtion2
3,4,condidtion2
3,4,condidtion2
3,4,condidtion2