相关疑难解决方法(0)

JSONDecodeError:期望值:第1行第1列(char 0)

Expecting value: line 1 column 1 (char 0)在尝试解码JSON 时遇到错误.

我用于API调用的URL在浏览器中工作正常,但在通过curl请求完成时会出现此错误.以下是我用于curl请求的代码.

错误发生在 return simplejson.loads(response_json)

    response_json = self.web_fetch(url)
    response_json = response_json.decode('utf-8')
    return json.loads(response_json)


def web_fetch(self, url):
        buffer = StringIO()
        curl = pycurl.Curl()
        curl.setopt(curl.URL, url)
        curl.setopt(curl.TIMEOUT, self.timeout)
        curl.setopt(curl.WRITEFUNCTION, buffer.write)
        curl.perform()
        curl.close()
        response = buffer.getvalue().strip()
        return response
Run Code Online (Sandbox Code Playgroud)

完全追溯:

追溯:

File "/Users/nab/Desktop/myenv2/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/Users/nab/Desktop/pricestore/pricemodels/views.py" in view_category
  620.     apicall=api.API().search_parts(category_id= str(categoryofpart.api_id), manufacturer = manufacturer, filter = filters, start=(catpage-1)*20, limit=20, sort_by='[["mpn","asc"]]')
File "/Users/nab/Desktop/pricestore/pricemodels/api.py" in search_parts
  176.         return simplejson.loads(response_json) …
Run Code Online (Sandbox Code Playgroud)

python api json curl

196
推荐指数
16
解决办法
46万
查看次数

使用Python迭代文件

我有一个问题要理解迭代文件,在这里我继续我在解释器上输入的内容和结果:

>>> f = open('baby1990.html', 'rU')
>>> for line in f.readlines():
...  print(line)
... 
# ... all the lines from the file appear here ...
Run Code Online (Sandbox Code Playgroud)

当我再次尝试迭代同一个打开的文件时,我什么也没得到!

>>> for line in f.readlines():
...  print(line)
... 
>>>
Run Code Online (Sandbox Code Playgroud)

根本没有输出,为了解决这个问题,我要关闭()文件,然后再打开它进行阅读!! 这是正常的行为吗?

python iteration file

51
推荐指数
3
解决办法
11万
查看次数

UnboundLocalError:从文件读取时在赋值之前引用的局部变量

我也试过寻找答案,但我不明白其他人类似问题的答案......

tfile= open("/home/path/to/file",'r') 

def temp_sky(lreq, breq):
    for line in tfile:
        data = line.split()
        if (    abs(float(data[0]) - lreq) <= 0.1 
            and abs(float(data[1]) - breq) <= 0.1):            
            T= data[2]
    return T
print temp_sky(60, 60)
print temp_sky(10, -10)
Run Code Online (Sandbox Code Playgroud)

我收到以下错误

7.37052488
Traceback (most recent call last):
File "tsky.py", line 25, in <module>
  print temp_sky(10, -10)
File "tsky.py", line 22, in temp_sky
  return T
UnboundLocalError: local variable 'T' referenced before assignment
Run Code Online (Sandbox Code Playgroud)

第一个print语句正常工作,但第二个不起作用.我尝试将T设为全局变量,但这使得两个答案都相同!请帮忙!

python

34
推荐指数
3
解决办法
15万
查看次数

为什么我不能在同一数据上迭代两次?

老实说,我在这里有点困惑,为什么我不能在相同的数据上迭代两次?

def _view(self,dbName):
    db = self.dictDatabases[dbName]
    data = db[3]

    for row in data:
        print("doing this one time")

    for row in data:
        print("doing this two times")
Run Code Online (Sandbox Code Playgroud)

这将打印出"一次这样做"几次(因为数据有几行),但它根本不会打印出"这样做两次"......

我第一次迭代数据工作正常,但第二次当我运行最后一个列表"for data in data"时,这没有返回...所以执行它一次工作但不是两次......?

仅供参考 - 数据是一个csv.reader对象(如果是这样的原因)......

python

34
推荐指数
4
解决办法
2万
查看次数

为什么文件读取后是空的?

我对 Python 很陌生。我想处理现有文件 ( exist_file),并创建它的副本。问题是,当我创建文件的副本时,它exist_file变成空的。

exist_file = open('some_pass/my_file.txt', 'r')
print exist_file.read() # Here the file is successfully printed
copy_of_file = open('new_copied_file.txt', 'w')
copy_of_file.write(exist_file.read())
print exist_file.read() # Here the file is empty
Run Code Online (Sandbox Code Playgroud)

为什么exist_file是空的?

python file

5
推荐指数
1
解决办法
6343
查看次数

Python read()函数返回空字符串

如果我在Python中输入:

open("file","r").read()
Run Code Online (Sandbox Code Playgroud)

有时它会将文件的确切内容作为字符串返回,有时它会返回一个空字符串(即使文件不为空).有人可以解释一下这取决于什么?

python file-io

4
推荐指数
2
解决办法
2万
查看次数

未绑定的变量和名称

根据python reference manual我们有

如果根本找不到名称,则会引发 NameError 异常。如果名称引用尚未绑定的局部变量,则会引发 UnboundLocalError 异常。UnboundLocalError 是 NameError 的子类。

我不明白什么时候UnboundLocalError抛出?因为

Python 缺少声明并允许名称绑定操作发生在代码块中的任何位置。

那么我们如何才能声明一个变量,而不去初始化她呢?

python variables namespaces

4
推荐指数
1
解决办法
1万
查看次数

读取同一文件Python的多个行

我试图在Python中多次读取一些文件的行.

我正在使用这种基本方式:

 with open(name, 'r+') as file:
                for line in file:
                    # Do Something with line
Run Code Online (Sandbox Code Playgroud)

这样工作正常,但是如果我想在每个行继续迭代,而我仍然打开我的文件,如:

 with open(name, 'r+') as file:
                for line in file:
                    # Do Something with line
                for line in file:
                    # Do Something with line, second time
Run Code Online (Sandbox Code Playgroud)

然后它不起作用,我需要打开,然后关闭,然后再次打开我的文件,使其工作.

with open(name, 'r+') as file:
                    for line in file:
                        # Do Something with line
with open(name, 'r+') as file:
                    for line in file:
                        # Do Something with line
Run Code Online (Sandbox Code Playgroud)

谢谢你的回答!

python for-loop file

4
推荐指数
1
解决办法
9396
查看次数

迭代python中的文件行

我有一个文件,其中有一些名称逐行列出.

gparasha-macOS:python_scripting gparasha$ cat topology_list.txt 
First-Topology
Third-topology
Second-Topology
Run Code Online (Sandbox Code Playgroud)

现在我试图迭代这些内容,但我无法这样做.

file = open('topology_list.txt','r')
print file.readlines()
for i in file.readlines():
    print "Entered For\n"
    print i

topology_list = file.readlines()
print topology_list
Run Code Online (Sandbox Code Playgroud)

file.readlines()以列表形式打印文件的行.所以我得到了这个:

 ['First-Topology\n', 'Third-topology\n', 'Second-Topology\n']
Run Code Online (Sandbox Code Playgroud)

但是,当我遍历此列表时,我无法这样做.

此外,当我将它分配给变量'topology_list'时,如倒数第二行并打印它.它给了我一个空列表.

[]
Run Code Online (Sandbox Code Playgroud)

所以我有两个问题.

我的做法有什么问题?怎么做到这一点?

python

3
推荐指数
1
解决办法
6963
查看次数

Python for循环中的重置行

我试图反复浏览几行文本文件.当我的for循环完成一遍文件后,我想从头开始重复该文件.有没有办法重置线计数器来实现这一目标?

infile=open("some_txt.txt","r")

choice=input("Choice:")
g=0

while choice!="close":
    if choice=='1':

        take_off=input("Take off:")
        for line in infile:
            x=line.split()
            if x[1]==take_off:
                print(x)
                g=g+1

        if g==0:
            print("No match.")

    elif choice=='2':

        take_off=input("Take off 2:")
        for line in infile:
            x=line.split()
            if x[2]==take_off:
                print(x)
                g=g+1

        if g==0:
            print("No match.")

    choice=input("Choice")
Run Code Online (Sandbox Code Playgroud)

python for-loop line reset

0
推荐指数
1
解决办法
3243
查看次数

标签 统计

python ×10

file ×3

for-loop ×2

api ×1

curl ×1

file-io ×1

iteration ×1

json ×1

line ×1

namespaces ×1

reset ×1

variables ×1