I've been having trouble with the following piece of Python code. It's running without errors, but it isn't exactly giving me the expected output; no files at all are being written.
for l in h:
r=l.rfind(",")+1
s=l[r:-2]
j=0
while j<18:
if s[j]==1:
f=codecs.open("genre_"+str(j)+".csv","w","utf-8")
f.write(l)
f.close()
j=j+1
h
is a list of strings, typically something like1,32,3.5,1112484819,000000000000101100\r\n
s
is the end of a string inh
, specifically the last number string- For each 1 in string
s
, the while loop writes the current string ofh
on a correspondingly indexedgenre_[num].csv
file
So, for instance, h[2]
is the example string above, and the files genre_12.csv
, genre_14.csv
and genre_15.csv
should be written with it. The thing is, after running the code, these genre files aren't even being created.
I've tried other variations of the code ('with codecs.open as f:') but fared no better. Am I missing something obvious?