{"action":"created","issue":{"url":"https://api.github.com/repos/josuerojasrojas/csv-yaml/issues/9","repository_url":"https://api.github.com/repos/josuerojasrojas/csv-yaml","labels_url":"https://api.github.com/repos/josuerojasrojas/csv-yaml/issues/9/labels{/name}","comments_url":"https://api.github.com/repos/josuerojasrojas/csv-yaml/issues/9/comments","events_url":"https://api.github.com/repos/josuerojasrojas/csv-yaml/issues/9/events","html_url":"https://github.com/josuerojasrojas/csv-yaml/issues/9","id":308342330,"number":9,"title":"python3 script","user":{"login":"mathewfer","id":15272931,"avatar_url":"https://avatars1.githubusercontent.com/u/15272931?v=4","gravatar_id":"","url":"https://api.github.com/users/mathewfer","html_url":"https://github.com/mathewfer","followers_url":"https://api.github.com/users/mathewfer/followers","following_url":"https://api.github.com/users/mathewfer/following{/other_user}","gists_url":"https://api.github.com/users/mathewfer/gists{/gist_id}","starred_url":"https://api.github.com/users/mathewfer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mathewfer/subscriptions","organizations_url":"https://api.github.com/users/mathewfer/orgs","repos_url":"https://api.github.com/users/mathewfer/repos","events_url":"https://api.github.com/users/mathewfer/events{/privacy}","received_events_url":"https://api.github.com/users/mathewfer/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-03-25T11:36:22Z","updated_at":"2018-04-02T21:08:47Z","closed_at":null,"author_association":"NONE","body":"Hi\r\n\r\nI tried to convert this script to Python 3 as I only have python3. I tried various ways but it doe not work.\r\nAny help to get this to Python3 is much appreciated.\r\n\r\nSee below;\r\n\r\nmathew@Tools-SRV:~$ python3 pycsv-toyaml-5-1.py -i CSVfile -o YAMLfile\r\n```\r\nTraceback (most recent call last):\r\n  File \"pycsv-toyaml-5-1.py\", line 83, in <module>\r\n    main()\r\n  File \"pycsv-toyaml-5-1.py\", line 80, in main\r\n    singleCSV(csvFile, output)\r\n  File \"pycsv-toyaml-5-1.py\", line 40, in singleCSV\r\n    csvToYaml(csvFile, output)\r\n  File \"pycsv-toyaml-5-1.py\", line 14, in csvToYaml\r\n    stream = file(output, 'w')\r\nNameError: name 'file' is not defined\r\n```\r\nmathew@Tools-SRV:~$ cat pycsv-toyaml-5-1.py\r\n```\r\nimport yaml\r\nimport os\r\nimport csv\r\nimport urllib.request\r\n#import urllib2 # i used for online csv\r\nimport getopt\r\nimport sys\r\n# https://stackoverflow.com/questions/16283799/how-to-read-a-csv-file-from-a-url-with-python\r\n\r\nroot = os.getcwd()\r\n\r\n# takes a csvFile name and output file name/path\r\ndef csvToYaml(csvFile, output):\r\n    stream = file(output, 'w')\r\n    csvOpen = csv.reader(csvFile)\r\n    keys = next(csvOpen)\r\n    for row in csvOpen:\r\n        yaml.dump([dict(zip(keys, row))], stream, default_flow_style=False)\r\n\r\n# converts single url file\r\ndef urlCSV(url, output=None):\r\n    csvFile = urllib2.urlopen(url)\r\n    output = output if output else root+'/'+(url.split('/')[-1].replace('.csv','.yml'))\r\n    csvToYaml(csvFile, output)\r\n\r\n# converts all csv file in this folder\r\ndef localCSV(folder=root):\r\n    # print folder\r\n    for f in os.listdir(folder):\r\n        if f.endswith('.csv'):\r\n            csvFile = os.path.join(folder, f)\r\n            output = os.path.join(folder, f.replace('.csv','.yml'))\r\n            print (output)\r\n            singleCSV(csvFile, output)\r\n\r\n# converts only one csv file\r\ndef singleCSV(csvFile, output=None):\r\n    output = output if output else root+'/'+(csvFile.split('/')[-1].replace('.csv','.yml'))\r\n    with open(csvFile, 'rb') as csvFile:\r\n        csvToYaml(csvFile, output)\r\n\r\n# print -h --help\r\ndef usage():\r\n    print (\"\\nUsage:\\n-i --input: path/link of file (if link use url flag)\\n-o --output: path name of output, if left out will convert in this folder using its name as output\\n-u --url: url flag indicating input is a url and should be treated as such\\n-f --folder: flag indicating input is directory/folder and should be treate as such\\n-h --help: print this/help stuff.....\")\r\n\r\ndef main():\r\n    try:\r\n        opts, args = getopt.getopt(sys.argv[1:], 'hi:o:uf', ['help', 'input=','output=', 'url', 'folder'])\r\n    except getopt.GetoptError as err:\r\n        print (str(err))\r\n        usage()\r\n        sys.exit(2)\r\n    csvFile = None\r\n    output = None\r\n    url = False\r\n    folder = False\r\n    if len(opts) == 0:\r\n        localCSV()\r\n        exit()\r\n    for o, a in opts:\r\n        if o in ('-h', '--help'):\r\n            usage()\r\n            sys.exit()\r\n        elif o in ('-i', '--input'):\r\n            csvFile = a\r\n        elif o in ('-o', '--output'):\r\n            output = a\r\n        elif o in ('-u', '--url'):\r\n            url = True\r\n        elif o in ('-f', '--folder'):\r\n            folder = True\r\n        else:\r\n            print ('unhandled option')\r\n    if url:\r\n        urlCSV(csvFile, output)\r\n        exit()\r\n    elif folder:\r\n        localCSV(csvFile)\r\n        exit()\r\n    singleCSV(csvFile, output)\r\n\r\nif __name__ in (\"__main__\", \"csvyml\"):\r\n    main()\r\n```\r\nmathew@Tools-SRV:~$\r\n\r\n\r\nMathew"},"comment":{"url":"https://api.github.com/repos/josuerojasrojas/csv-yaml/issues/comments/378047302","html_url":"https://github.com/josuerojasrojas/csv-yaml/issues/9#issuecomment-378047302","issue_url":"https://api.github.com/repos/josuerojasrojas/csv-yaml/issues/9","id":378047302,"user":{"login":"josuerojasrojas","id":10749061,"avatar_url":"https://avatars0.githubusercontent.com/u/10749061?v=4","gravatar_id":"","url":"https://api.github.com/users/josuerojasrojas","html_url":"https://github.com/josuerojasrojas","followers_url":"https://api.github.com/users/josuerojasrojas/followers","following_url":"https://api.github.com/users/josuerojasrojas/following{/other_user}","gists_url":"https://api.github.com/users/josuerojasrojas/gists{/gist_id}","starred_url":"https://api.github.com/users/josuerojasrojas/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/josuerojasrojas/subscriptions","organizations_url":"https://api.github.com/users/josuerojasrojas/orgs","repos_url":"https://api.github.com/users/josuerojasrojas/repos","events_url":"https://api.github.com/users/josuerojasrojas/events{/privacy}","received_events_url":"https://api.github.com/users/josuerojasrojas/received_events","type":"User","site_admin":false},"created_at":"2018-04-02T21:08:47Z","updated_at":"2018-04-02T21:08:47Z","author_association":"OWNER","body":"I am updating the code for it, I haven't tested it a lot but so far it works with the local files I tested and  url. Here is the code I have so far. I will update the repo when I clean the code a bit.\r\n\r\n```python\r\nimport yaml\r\nimport os\r\nimport csv\r\nimport requests\r\nimport codecs\r\n\r\n# import urllib2 # i used for online csv\r\n# https://stackoverflow.com/questions/2792650/import-error-no-module-name-urllib2 for python 3\r\nfrom urllib.request import urlopen\r\nimport getopt\r\nimport sys\r\n# https://stackoverflow.com/questions/16283799/how-to-read-a-csv-file-from-a-url-with-python\r\n\r\nroot = os.getcwd()\r\n\r\n# takes a csvFile name and output file name/path\r\ndef csvToYaml(csvFile, output):\r\n    stream = open(output, 'w',encoding=\"utf-8\")\r\n    # https://stackoverflow.com/questions/18897029/read-csv-file-from-url-into-python-3-x-csv-error-iterator-should-return-str\r\n    # need to decode bytes\r\n    csvOpen = csv.reader(codecs.iterdecode(csvFile, 'utf-8'))\r\n    keys = next(csvOpen)\r\n    print(keys)\r\n    # exit()\r\n    for row in csvOpen:\r\n        yaml.dump([dict(zip(keys, row))], stream, default_flow_style=False)\r\n\r\n# converts single url file\r\ndef urlCSV(url, output=None):\r\n    csvFile = urlopen(url)\r\n    output = output if output else root+'/'+(url.split('/')[-1].replace('.csv','.yml'))\r\n    csvToYaml(csvFile, output)\r\n\r\n# converts all csv file in this folder\r\ndef localCSV(folder=root):\r\n    # print folder\r\n    for f in os.listdir(folder):\r\n        if f.endswith('.csv'):\r\n            csvFile = os.path.join(folder, f)\r\n            output = os.path.join(folder, f.replace('.csv','.yml'))\r\n            print(output)\r\n            singleCSV(csvFile, output)\r\n\r\n# converts only one csv file\r\ndef singleCSV(csvFile, output=None):\r\n    output = output if output else root+'/'+(csvFile.split('/')[-1].replace('.csv','.yml'))\r\n    with open(csvFile, 'rb') as csvFile:\r\n        csvToYaml(csvFile, output)\r\n\r\n# print -h --help\r\ndef usage():\r\n    print('\\nUsage:\\n-i --input: path/link of file (if link use url flag)\\n-o --output: path name of output, if left out will convert in this folder using its name as output\\n-u --url: url flag indicating input is a url and should be treated as such\\n-f --folder: flag indicating input is directory/folder and should be treate as such\\n-h --help: print this/help stuff.....')\r\n\r\ndef main():\r\n    try:\r\n        opts, args = getopt.getopt(sys.argv[1:], 'hi:o:uf', ['help', 'input=','output=', 'url', 'folder'])\r\n    except getopt.GetoptError as err:\r\n        print(str(err))\r\n        usage()\r\n        sys.exit(2)\r\n    csvFile = None\r\n    output = None\r\n    url = False\r\n    folder = False\r\n    if len(opts) == 0:\r\n        localCSV()\r\n        exit()\r\n    for o, a in opts:\r\n        if o in ('-h', '--help'):\r\n            usage()\r\n            sys.exit()\r\n        elif o in ('-i', '--input'):\r\n            csvFile = a\r\n        elif o in ('-o', '--output'):\r\n            output = a\r\n        elif o in ('-u', '--url'):\r\n            url = True\r\n        elif o in ('-f', '--folder'):\r\n            folder = True\r\n        else:\r\n            print('unhandled option')\r\n    if url:\r\n        urlCSV(csvFile, output)\r\n        exit()\r\n    elif folder:\r\n        localCSV(csvFile)\r\n        exit()\r\n    singleCSV(csvFile, output)\r\n\r\nif __name__ in (\"__main__\", \"csvyml\"):\r\n    main()\r\n```"}}
{"actor":{"display_login":"josuerojasrojas"}}