How to read and get perticular requried data from .txt file from python code

Please share your views regarding how to read and get perticular data from .txt file from a python code. I was able to do it for .conf file , but not able to do with .txt file

If you’ve read a conf file before, reading a txt or any other file for that matter is no different. Just open the file and read data from it and close it after.

Hi Kausik,

I’m sorry, your question is so vague that I don’t know where to begin.
Your question is like:

“Please share your views regarding how to buy products from shops. I was
able to do it from one shop, but not another.”

Can you be more specific about your problem please?

  • what sort of .txt file do you have?
  • where is it stored?
  • what is inside the .txt file?
  • how do you know?
  • how did you try to read data from it?
  • what happened when you tried?
  • did you get an error, or something else?

Some more questions we might ask:

  • how did you read data from the .config file?
  • can you try doing the same thing with the .txt file?
  • what happens when you do?

Hello for .conf file i used
parser = configparser.ConfigParser

its not working for .txt file.
it shows configparser.MissingSectionHeaderError: File contains no section headers.
file: ‘prop.txt’, line: 1

Process finished with exit code 1

Hi but for .conf file i used parser = configparser.ConfigParser. but its not working for .txt file

in .txt i specificed some data line name = ******** , id = ******** etc . Its has 3 names and 3 ids . In the python code i will give an id as input then it should get that id’s name . With .conf i was able to read and get that requried info , but i dont know why its not working with .txt file

Alright, the general way for opening files in python is by using the builtin open() function. If your text file is structured in some way, you’ll have to parse it in order to extract meaning out of it.

file = open(filename,'r')
data =

That’s basically how your program might go.

In the .txt file i has name = ****, id = ***** , etc
When i run the python , i will give id as an input the it get the perticular id’s name , etc.
For .conf file i used
parser = configparser.ConfigParser“name.conf”)

but it doesnt work for txt file

That is because your .txt file is missing the section headers.

Look at the .conf file, and you will see section headers in square
brackets like:

name = Kausik
address = "1234 Main Street, City"

Your .txt file has no section headers. It needs them, otherwise you
cannot use the ConfigParser with it.

You cannot just point ConfigParser at some random text file containing
any old stuff and expect it to do miracles. You have to use a text file
that is formatted the same way ConfigParser expects it to be formatted.

I have got it

You need to put in a section called ‘default’.

Python is not lying to you. If it says that there is no section called
default, there is no section called default.

Check that you have a section marker and that it is spelled correctly.