0

i want to extract a certain keyword called "Visual Impairment" from a huge text file. i know this is probably a very minor issue but i am still fairly new to Python so please help :)

an example of the text file: {'Sensory and Communication': {'Visual Impairment': 'No', 'Hearing Impairment': 'YesMild', 'Communication': 'YesVerbal'}

i want it to be such that 'No = 0' and the output to be

Visual Impairment: 0

my code now is

file_path ='C:\\Users\L31308\Desktop\\IFA.txt'

with open(file_path,'r') as f:

data = f.read()



def sensoryAndCommunication(visualImpairment, hearingImpairment, communication):

   if visualImpairment == 'Visual Impairment':

       if visualImpairment == 'No':

           visualImpairment = 0

           print("Visual Impairment" + visualImpairment)
1
  • There's an indentation error in your with statement. Commented Oct 18, 2018 at 2:42

2 Answers 2

0

Your code is a bit all over the place. Besides the indentation problem stated before in the comment, your function does not make much sense. You are checking if visualImpairment == 'Visual Impairment': and then right after you are checking if visualImpairment == 'No': There is no way a variable can have two different values. Besides that, you are not even using the data you read to extract information from. It seems like you are a little confused about the way it needs to be done.

1- It appears that the file you are reading is written in a JSON format, if that is the case, I think you should try parsing the text to extract the information you are looking for. Try this Link for a start or look for other tutorials

2- Once you learn how to parse correctly, use that to extract the information you are looking for and then use an if-condition to check if the value is 'Yes' or 'No'.

Good luck

Sign up to request clarification or add additional context in comments.

Comments

0

it appears that you are trying to read json object from text file. if that is the case then your json objects in 'IFA.txt'should be formatted as follows:

{"Sensory and Communication":{"Visual Impairment":"No","Hearing Impairment":"YesMild","Communication":"YesVerbal"}}

note the double-quotes.then it will be easy to parse the file. follow this so link so that you will have clear picture .try the following code:

import json
with open('IFA.txt') as f:
   json_data = json.load(f)
   # in python single-quote and double-quote makes no difference
   if  json_data['Sensory and Communication']['Visual Impairment']=='No':
       print json_data['Sensory and Communication']['Visual Impairment']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.