I have a very basic question in python. I want to split the items in the following list and print it in a text file.
import pandas as pd
s = ['"9-6": 1', ' "15-4": 1', ' "12-3": 1', ' "8-4": 1', ' "8-5": 1', ' "8-1": 1']
print type(s)
for i in s:
j = i.split(',')
with open("out.txt","w") as text_file:
text_file.write("{}".format(j))
However, my code only prints the last value. Clearly, it is not taking the last lines inside the for loop block. Can anyone point where am I going wrong? Thanks!
j = i.split(','), you're iterating through each string in your array and then splitting each string into substrings separated by commas, but there are no commas in any of the strings.