从命令行调用 python 脚本给出“NameError:名称'convert_json_to_csv'未定义”

问题描述 投票:0回答:1

我有 python 脚本“convert_json_to_csv.py”。 当我按回车键时,出现错误:“NameError:名称'convert_json_to_csv'未定义”。

enter image description here

以下是脚本内容:

import pandas as pd

json_file_path = 'trulab.json' # Path to your JSON file
csv_file_path = 'trulab.csv' # Path where CSV file will be saved

def json_to_csv(json_file_path, csv_file_path):
    # Read JSON file into a dataframe
    df = pd.read_json(json_file_path, lines=True)
    
    # Save dataframe to a CSV file, delimited by a pipe
    df.to_csv(csv_file_path, index=False, sep='|')

json_to_csv(json_file_path, csv_file_path)

请指教。 感谢您的时间和帮助。 此致, 唐纳德

我尝试从命令行运行脚本 我期待 python 运行脚本 但它似乎“认为”我的脚本是一个未定义的变量。 我是Python新手。

windows command-line
1个回答
0
投票

您启动 python 的命令是错误的,您调用了 Python 交互式 shell。您应该提供脚本的名称来启动它:

python convert_json_to_csv.py
© www.soinside.com 2019 - 2024. All rights reserved.