'Writting string to a File - 'str' object is not callable
i am generating a Json-like structure and I need write it to a File, any format of file, but i am getting this error. I have tried written it as Json and string without any luck.
import csv
score = []
test = ''
with open("score_test.txt") as f:
spam = csv.reader(f, delimiter='|')
for line in spam:
score.append(line)
open = """{
"info": {
"_postman_id": "",
"name": "GD-API-Testing-Update",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"name": "Score_Tests",
"item": [
{
"name": "Score_Ok",
"item": ["""
for num, row in enumerate(score):
test += """
{
"name": "Score_Ok_%d",
"event": [
{
"listen": "test",
"script": {
"exec": [
"pm.test(\"Status test of API-GD.\", function () {",
" pm.response.to.have.status(200);",
"});",
"",
"pm.test(\"Score test.\", function () {",
" pm.response.to.not.be.error;",
" const responseJson = pm.response.json();",
" pm.expect(responseJson.gd_result).to.eql("%s");",
"});"
],
"type": "text/javascript"
}
}
],
"request": {
"method": "GET",
"header": [
{
"key": "X-Access-Key-Id",
"value": "{{X-Access-Key-Id}}",
"type": "text"
},
{
"key": "X-Secret-Access-Key",
"value": "{{X-Secret-Access-Key}}",
"type": "text"
},
{
"key": "Accept-Version",
"value": "{{Accept-Version}}",
"type": "text"
}
],
"url": {
"raw": "{{URL-API-GD}}/api/model/:model_id/mdn/:mdn",
"host": [
"{{URL-API-GD}}"
],
"path": [
"api",
"model",
":model_id",
"mdn",
":mdn"
],
"variable": [
{
"key": "model_id",
"value": "{{Test-Model}}"
},
{
"key": "mdn",
"value": "%s"
}
]
}
},
"response": []
}""" % (num, row[1], row[0])
if num != len(score) - 1:
test += ","
close = """ ]
}
]
}"""
sample = open + test + close
with open("sample.txt", "a") as f:
f.write(sample)
This is the error:
Traceback (most recent call last):
File "/Users/user/Documents/training/TestForPostman/testGenerator.py", line 111, in <module>
with open("sample.txt", "a") as f:
TypeError: 'str' object is not callable
The for Loop it is appending the generated string I need, and finally closed the structure at the end. I already checked the json structure and it is ok.
Solution 1:[1]
You are using open as variable in your code sample = open + test + close which is causing issue. Avoid using any reserved word as a variable.
Rename the open variable with something else then it will work.
Solution 2:[2]
You are trying to focus() on a text string, rather than a jQuery HTML element.
QIDFind= $(this).closest('tr').children('#tableQID').html(); is the content of that element, not the element
QIDFind= $(this).closest('tr').children('#tableQID') will give you the control(s), you then need to put .html() just where you need it
There are other issues. You are appending rows where the child element have the same ID in each row. ID need to be unique on the page, so you should probably switch to classes or data- attributes.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | PB22 |
| Solution 2 | Gone Coding |
