Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python解析函数中出现错误_Python_Json_Function_Parsing - Fatal编程技术网

python解析函数中出现错误

python解析函数中出现错误,python,json,function,parsing,Python,Json,Function,Parsing,我是Python新手,在运行代码时出现了一些错误 我有一个Amazon数据集,它的格式是JSON文件 (请参见下面的json格式) 我使用的命令是由数据发送者提供的,它将上面的JSON文件转换为“strict JSON”文件(根据数据发送者,原始JSON文件不是strict JSON) 他们提供的命令如下: import json import gzip def parse(path): g = gzip.open(path, 'r') for l in g: yield js

我是Python新手,在运行代码时出现了一些错误

我有一个Amazon数据集,它的格式是JSON文件 (请参见下面的json格式)

我使用的命令是由数据发送者提供的,它将上面的JSON文件转换为“strict JSON”文件(根据数据发送者,原始JSON文件不是strict JSON)

他们提供的命令如下:

import json
import gzip

def parse(path):
  g = gzip.open(path, 'r')
  for l in g:
    yield json.dumps(eval(l))

f = open("output.strict", 'w')
for l in parse("reviews_Video_Games.json.gz"):
  f.write(l + '\n')
我只更改了路径,将JSON文件的目录加上引号(例如,“C:\Users\daisy\Research\study\Amazon\reviews\u Video\u Games.JSON.gz”)

例如,我运行的代码如下所示:

import json
import gzip

def parse(C:\Users\daisy\Research\study\Amazon\reviews_Video_Games.json.gz):
  g = gzip.open(C:\Users\daisy\Research\study\Amazon\reviews_Video_Games.json.gz, 'r')
  for l in g:
    yield json.dumps(eval(l))

f = open("output.strict", 'w')
for l in parse("reviews_Video_Games.json.gz"):
  f.write(l + '\n')
但是,我得到以下错误:

C:\Users\daisy\AppData\Local\Programs\Python\Python36-32>python C:\Users\daisy\AppData\Local\Programs\Python\strict_json.py
  File "C:\Users\daisy\AppData\Local\Programs\Python\strict_json.py", line 4
def parse("C:\Users\daisy\Research\study\Amazon\reviews_Video_Games.json.gz"):
                                                                                ^
SyntaxError: invalid syntax
你知道语法有什么问题吗

同样,原始代码是由数据发送者提供的,因此我非常确定代码是正确的。当我将“路径”更改为文件目录时,我想我做错了什么


多谢各位

您不能定义这样的函数

def parse(file_path):
  g = gzip.open(file_path, 'r')
  for l in g:
    yield json.dumps(eval(l))

parse(r"C:\Users\daisy\Research\study\Amazon\reviews_Video_Games.json.gz")
尽管您可以这样设置默认值:

def parse(file_path=r"C:\Users\daisy\Research\study\Amazon\reviews_Video_Games.json.gz"):
  g = gzip.open(file_path, 'r')
  for l in g:
    yield json.dumps(eval(l))

parse()
编码问题更新

>“C:\Users\daisy\Research\study\Amazon\reviews\u Video\u Games.json.gz”
文件“”,第1行
SyntaxError:(unicode错误)'UnicodeScape'编解码器无法解码位置2-3中的字节:截断\UXXXXXXXX转义
>>>“C:\\Users\\daisy\\Research\\study\\Amazon\\reviews\u Video\u Games.json.gz”
'C:\\Users\\daisy\\Research\\study\\Amazon\\reviews\u Video\u Games.json.gz'
>>>r“C:\Users\daisy\Research\study\Amazon\reviews\u Video\u Games.json.gz”
'C:\\Users\\daisy\\Research\\study\\Amazon\\reviews\u Video\u Games.json.gz'

def parse(C:\Users\daisy\Research\study\Amazon\reviews\u Video\u Games.json.gz):
??该函数是按原样使用的<代码>路径是调用函数时提供的参数,而不是函数定义本身应该更改的参数。它不是这样工作的。调用函数时,必须将值替换为parse中的l(r“C:\Users\daisy\Research\study\Amazon\reviews\u Video\u Games.json.gz”):另外,不要忘记
“…”
,最好将其设置为
r
原始字符串。@WillemVanOnsem你好,Willem,我对python真的很陌生,不知道我在括号里放了什么。我认为应该将路径替换为json文件的目录路径。你能告诉我它有什么问题吗?这是函数定义的官方语法。在def语句中,python需要一个参数列表,通常是以逗号分隔的标识符序列。您需要阅读python教程。谢谢,Ipiner。我运行了第二个代码,但得到了与下面相同的错误,即“SyntaxError:(unicode错误)‘UnicodeScape’编解码器无法解码位置2-3的字节:截断\UXXXXXXXX转义”。你知道如何解决这个问题吗?以后请保留原来的问题,并在底部添加新内容,以免过时任何答案。您的新错误是编码问题,请用r
r“C:\Users\daisy\Research\study\Amazon\reviews\u Video\u Games.json.gz”转义反斜杠或将字符串标记为原始字符串
def parse(file_path=r"C:\Users\daisy\Research\study\Amazon\reviews_Video_Games.json.gz"):
  g = gzip.open(file_path, 'r')
  for l in g:
    yield json.dumps(eval(l))

parse()
>>> "C:\Users\daisy\Research\study\Amazon\reviews_Video_Games.json.gz"
  File "<stdin>", line 1
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
>>> "C:\\Users\\daisy\\Research\\study\\Amazon\\reviews_Video_Games.json.gz"
'C:\\Users\\daisy\\Research\\study\\Amazon\\reviews_Video_Games.json.gz'
>>> r"C:\Users\daisy\Research\study\Amazon\reviews_Video_Games.json.gz"
'C:\\Users\\daisy\\Research\\study\\Amazon\\reviews_Video_Games.json.gz'