Dear coding community,
Im generating multiple JSON file throught a python script.
f1= open('readingfile.txt', 'a')
def yt():
for line in f1:
a = print(line)
def create(num):
num = 0
num = 0 1
program = open(str(p) ".json", "w")
BASE_IMAGE_URL = "ipfs://aBcDeFgHiKjKlMnOpQrStUvWxYz/"
writejson = {
"name": "whatever",
"description": "Most wholesome NFT",
"image": BASE_IMAGE_URL str(i) ".json",
"youtube": yt
}
program.write(str(writejson))
for p in range(1,11):
create(p)
The script generate X amount of JSON file (define from the loop 1,11).
In those file contain 4 lines : Name , Description , image and Youtube
'Name' and 'description' dont change.
'Image' take an internet link(some nft metadata) , and add 1 (from the loop function)
The 'youtube' line is SUPPOSE to read from 'readingfile.txt' and write in the youtube line. The 'reading file.txt' file
This is the result of the generated JSON file:
{"name": "BBR", "description": "Most wholesome NFT", "image": "ipfs://aBcDeFgHiKjKlMnOpQrStUvWxYz/1.json", "youtube": <function yt at 0x0000025D4B89EF80>}
Everything working fine , except the youtube line , instead of printing the result of the function , its just printing "<function yt at 0x0000025D4B89EF80". How do i make it to print the result of the function , can i get a lil help from the community ?
Cheers
CodePudding user response:
I believe your yt function isn't returning anything, and it seems like you want this:
def yt():
for line in f1:
print(line)
CodePudding user response:
You are only providing the name of the function, not the function call! Instead of typing 'yt', you need to tell the program to actually run the function by providing two parentheses after the function name, like such:
yt --> function name, not actually telling the function to run
yt() --> function call, telling the function to run
Another issue you may be facing when you call the function is it isn't going to actually place the value in your JSON object unless you return a value from the function. So, we need to add a return statement from the function.
Also, some good programming tips:
- Always remember to close a file you have opened. When you use f1 = open(something), you need to close the file by calling f1.close() at the very end. Leaving files open can create data leaks.
- When reading from a file, it can be useful to open, immediately read all the contents, and then close the file. If we store all the contents in a separate variable, it can help speed up a program since we don't need to open and close a file a bunch of times.
If you modify your code to be the following:
import json
f1 = open('readingfile.txt', 'r')
f1_contents = f1.readlines()
f1.close()
def yt(line_num):
return f1_contents[line_num].replace('\n', '')
def create(p):
# num = 0
# num = 0 1
BASE_IMAGE_URL = "ipfs://aBcDeFgHiKjKlMnOpQrStUvWxYz/"
writejson = {
"name": "whatever",
"description": "Most wholesome NFT",
"image": BASE_IMAGE_URL str(p 1) ".json",
"youtube": yt(p)
}
program = open(str(p) ".json", "w")
json_data = json.dumps(writejson)
program.write(json_data)
program.close()
for p in range(0, 9):
create(p)
*NOTE: This will only work if the number of lines in your 'readingfile' matches your loop control in your 'for p in range(1, 11)' loop. That is, if there are less than 10 lines in your file, because your loop is running 10 times, your program will crash.
Then you can achieve what you are trying to do. Note that I also imported the json module at the top and replaced how you write to the json file. Python has built in json file libraries that assist in both reading and writing to json files easily!
When returning the line from your yt() function as well, you need to replace the newline character "\n" with a blank space, or you will end up with a Youtube link that looks something like "www.youtube.com\stuff\ \n"
I also modified how you read contents from the 'readingfile.' Now, you read all the lines from the file and store them in a list at the beginning, and simply take the line you need when you get to that step.
Finally, I modified how your loop works. I assume you want the loop to keep track of the current step you are on. Because of this, I commented out the num = 0 and the num = 0 1 (as those seemed redundant) and simply replaced the create() function parameter to track the current step you are on. Also, python is 0 indexed (meaning 0 is the start of a list, not 1), so you want your loop to start at 0 to access the first item of the list, and go up to 10.
I hope this helps!