-3

I came across this script I had help with a while ago. Basically taking a csv and exporting it JSON format but how do I go about running the code? I tried doing python hello.py

import csv
import json

with open('sea-lead-csv.csv') as fd:
    reader = csv.DictReader(fd)
    data = []
    for row in reader:
        row['Name'] = row.pop('No')
        data.append(row)

    print(json.dumps(data, indent=2))

enter image description here

  • From where did u send the command ? Is it from any text editor or a terminal like cmd? – httpanand May 16 '22 at 02:48
  • Sorry I am on vscode. I have the csv file I want to covert open in another tab and pressed the play function – flexwithfrank May 16 '22 at 02:49
  • 1) what's the name of the file? 2) What errors did you get? – Keith May 16 '22 at 02:49
  • Is the file opened in read mode? Try `with open('sea-lead-csv.csv', 'r') as fd:` – Geeky Quentin May 16 '22 at 02:50
  • Is that csv file on the same path as of your python file? – httpanand May 16 '22 at 02:53
  • No such file or directory: 'sea-lead-csv.csv' is an error , but where is this file supposed to live? – flexwithfrank May 16 '22 at 02:54
  • By the same do you mean in the same folder? – flexwithfrank May 16 '22 at 02:54
  • You need to make sure you have the proper path to the csv file. – Matthew Hartman May 16 '22 at 02:55
  • 2
    @flexwithfrank Yes sry .. same folder . Otherwise you have to provide the path to your csv file in the code .. – httpanand May 16 '22 at 02:59
  • 1
    @Geeky No need to specify `open(..., 'r')`, since that's the default option. Either way, as we can see now, that's not related to the problem. – wjandrea May 16 '22 at 03:03
  • 2
    [Please don't post pictures of errors or other text](https://meta.stackoverflow.com/q/285551/4518341). Instead, copy the error itself and use the formatting tools like [code formatting](/editing-help#code). In this case it also helps to include the line above the error (since it includes the working directory and full paths to the Python interpreter and script). For more tips, see [mre] and [ask]. – wjandrea May 16 '22 at 03:10
  • 1
    Since the given file name has no path, it's relative, the script is assuming the file is in the current working directory — but apparently it's not there. You can see what the current working directory is with `import os`, followed by `print(os.getcwd())`. To fix the problem you need to specify the full path to the file which means prefixing the file name with what `getcwd()` displays. – martineau May 16 '22 at 03:10
  • 1
    Perhaps see also [What exactly is current working directory?](https://stackoverflow.com/questions/45591428/what-exactly-is-current-working-directory) – tripleee May 16 '22 at 04:27
  • Thank you, I created the CSV in the same directory and used the script with added - sys.stdout = open('kiosk-drip.txt', 'w') - since the output on the terminal only shows for about 50 results. Thanks for the help! – flexwithfrank May 16 '22 at 15:17
  • : ) Happy to hear – httpanand May 16 '22 at 15:19

0 Answers0