List1 = ['ROLLER,abc','BOLT,s']
List2 = ['TYPE , ROL,','DMET, LENGTH, THRES Tgg, GRE B, HEAD X,']
I want to save the lists data into CSV like this using python
Heading 1 | Heading 2 |
---|---|
Roller,abc | Type |
Roller,abc | Rol |
Bolt,s | DMET |
Bolt,s | Lenght |
Bolt,s | Thress tag |
Bolt,s | GRE B |
Bolt,s | Head X |
CodePudding user response:
Quick response without csv module
This is not the best method!
list1 = ['ROLLER','BOLT']
list2 = ['TYPE , ROL','DMET, LENGTH, THRES Tgg, GRE B, HEAD X,']
file = open("test.csv", mode="w")
for i in range(len(list1)):
for y in [_.strip() for _ in list2[i].split(",")]:
file.write(list1[i] "," y "\n")
file.close()
CodePudding user response:
It is best to use open
as context manager with with
operator.
It will close file if IndexError
exception happens.
with open('filename.txt', 'w') as file:
for i, col_1 in enumerate(List1):
for col_2 in List2[i].split(','):
file.write(f"{col_1},{col_2.strip()}\n")