# # Reads csv file to convert to opml file. # # Rolf Redford # # 8/21/2021 # # 1.0 initial version. #!/usr/bin/python import sys import os.path from os import path from xml.sax.saxutils import escape if not len(sys.argv) == 3: exit("Need csvtoopml.py \n"); # filenames infil = str(sys.argv[1]); outfil = str(sys.argv[2]); # files inf = "" outf = "" # opml variables otitle = "" ochannelID = "" # open file with handling try: inf = open(infil, "r", encoding="utf8") except ValueError: print("Input file error: " & ValueError) # check file exist if it dont, open file, # unless user want to overwrite anyway. if path.exists(outfil): val = raw_input("Overwrite " + outfil + " file? (y/n):") if not val == "y": exit(outfil + " file exists.") try: outf = open(outfil, "w", encoding="utf8") except ValueError: inf.close() # since already opened print("Output file error: " & ValueError) # Opening lines in file outf.write("\n") outf.write("\n") outf.write(" \n") outf.write(" youtube data to OPML export\n") outf.write(" \n") outf.write("\n") outf.write(" \n") # now process file, stripping needed info and write in opml file. for x in inf: infile = x if not "Channel Id" in infile: work = infile.split(",") # always at array #3. if len(work) == 3: ochannelID = work[0]; # file might have errors but not likely. otitle = escape(work[2]) # Escape XML special characters outf.write(" \n") # now close tags outf.write(" \n") #youtube sub... tag outf.write("\n") outf.write("\n") # close files inf.close() outf.close()