小编lea*_*123的帖子

使用json.loads将文本文件读回字典

我通过以下方式将我的Python脚本的输出传输到文件output.txt,该脚本访问实时推文推文:

$python scriptTweet.py > output.txt
Run Code Online (Sandbox Code Playgroud)

最初,脚本返回的输出是一个写入文本文件的字典.

现在我想使用output.txt文件来访问存储在其中的推文.但是,当我使用此代码使用json.loads()将output.txt中的文本解析为python字典时:

tweetfile = open("output.txt")
pyresponse = json.loads('tweetfile.read()')
print type(pyresponse)
Run Code Online (Sandbox Code Playgroud)

弹出此错误:

    pyresponse = json.loads('tweetfile.read()')
  File "C:\Python27\lib\json\__init__.py", line 326, in loads
    return _default_decoder.decode(s)
  File "C:\Python27\lib\json\decoder.py", line 366, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Python27\lib\json\decoder.py", line 384, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
Run Code Online (Sandbox Code Playgroud)

我应该如何将文件output.txt的内容再次转换为字典?

python json

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

在c ++中查找字符串的所有排列

以下是打印给定字符串的所有排列的代码.代码编译但不打印任何内容.

using namespace std;

void RecPermute(string, string);

int main() {
    RecPermute("", "abc");
    return 0;
}

void RecPermute(string soFar, string rest) {
    if (rest == " ") {
        cout << soFar << endl;
    } else {
        for(int i=0; i<rest.length(); i++) {
            string next = soFar + rest[i];
            string remaining = rest.substr(0, i) + rest.substr(i+1);
            RecPermute(next, remaining);
    }
    }
}
Run Code Online (Sandbox Code Playgroud)

需要修复什么?

c++ string

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

How to move contents from one stack to another?

我想将内容从堆栈收件箱移动到发件箱,然后返回发件箱的顶部.但pop的返回类型是void,因此代码抛出一个错误:void值不应该被忽略.

如何移动价值观?

这是代码:

template <class E>
class Queue
{

    private:
        stack<E> inbox;
        stack<E> outbox;

    public:
     void enqueue(E item) {
        inbox.push(item);
    }

    E dequeue() {
        if (outbox.empty()) {
            while (!inbox.empty()) {
                outbox.push(inbox.pop());
            }
        }

        return outbox.pop();
    }

};
Run Code Online (Sandbox Code Playgroud)

c++

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

标签 统计

c++ ×2

json ×1

python ×1

string ×1