1

I want to read the above file foo.txt and read only UDE from the first line and store it in a variable then Unspecified from the second line and store it in a variable and so on. should I use read or readlines ? should I use regex for this ?? My below program is reading the entire line. how to read the specific word in the line ?

fo = open("foo.txt", "r+")
line = fo.readline()
left, right = line.split(':')
result = right.strip()
File_Info_Domain = result
print File_Info_Domain
line = fo.readline()
left, right = line.split(':')
result = right.strip()
File_Info_Intention = result
print File_Info_Intention
line = fo.readline()
left, right = line.split(':')
result = right.strip()
File_Info_NLU_Result = result
print File_Info_NLU_Result
fo.close()

2 Answers 2

1

You can use readline() (without s in name) to read line on-by-one, and then you can use split(':') to get value from line.

fo = open("foo.txt", "r+")

# read first line
line = fo.readline()

# split line only on first ':'
elements = line.split(':', 1) 

if len(elements) < 2:
    print("there is no ':' or there is no value after ':' ")
else:
    # remove spaces and "\n"
    result = elements[1].strip()
    print(result)

#
# time for second line
#

# read second line
line = fo.readline()

# split line only on first ':'
elements = line.split(':', 1)

if len(elements) < 2:
    print("there is no ':' or there is no value after ':' ")
else:
    # remove spaces and "\n"
    result = elements[1].strip()
    print(result)

# close
fo.close()
Sign up to request clarification or add additional context in comments.

9 Comments

I updated my code but I am getting the error as : Traceback (most recent call last): File ".\file.py", line 46, in <module> left, right = line.split(':') ValueError: too many values to unpack
That's because you have too many values in the third line, it's being splitted at each :
because you have more then one : in line and split create more then two elements - use elements = line.split(':') and then elements[1] to get element.
can you update that in the code. it is confusing. Is it correct - # read third line line = fo.readline() elements = line.split(':') elements[1] = elements result3 = right.strip() File_Info_NLU_Result = result3 print File_Info_NLU_Result
btw: you asked for first and second line - third needs modification - split need another argument to slip only on first ':'.
|
0

While you can use @furas response or regex, I would recommend you to use a config file to do this, instead of a plain txt. So your config file would look like:

[settings]
Domain=UDE
Intention=Unspecified
nlu_slot_details={"Location": {"literal": "18 Slash 6/2015"}, "Search-phrase": {"literal": "18 slash 6/2015"}

And in your python code:

import configparser

config = configparser.RawConfigParser()
config.read("foo.cfg")

domain = config.get('settings', 'Domain')
intention = config.get('settings', 'Intention')
nlu_slot_details = config.get('settings', 'nlu_slot_details')

4 Comments

I agree with you :) I was going to suggest json or yaml.
i am getting an error as : ImportError: No module named configparser
@akshay if you use Python 2.7 try ConfigParser
You need to install the package. You can do it with pip

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.