Home > Mobile >  is there a way to make the coad read a txt file and tell me what the each of the names gender is
is there a way to make the coad read a txt file and tell me what the each of the names gender is

Time:08-18

my code

import requests,json

name = ("hika")
content = requests.get(f"https://api.genderize.io/?name={name}").text
gender = json.loads(content)['gender']

print(f"Gender of {name} is {gender}")

I want to make it reads a txt file and tells me which names are males and which are females

CodePudding user response:

Here is a code reading names from path_to_txt_file.txt file:

import requests,json

with open("path_to_txt_file.txt", "r") as f:
  for line in f.readlines():
    name = line.strip()
    content = requests.get(f"https://api.genderize.io/?name={name}").text
    gender = json.loads(content)['gender']

    print(f"Gender of {name} is {gender}")
    with open(f"{gender}.txt", "a") as fw:
      fw.write(name   "\n")
  • Related