473,395 Members | 1,422 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

extracting part of a text in a file

Hi,
I am new to python. I have a file in my $HOME (say a.tcl). I want to extract something from it with a pattern and assign that to a variable. So:

Content of /home/mahmood/a.tcl is:
Expand|Select|Wrap|Line Numbers
  1. ...
  2. set ckptdir $::env(SIM)/checkpoints
  3. ...
Suppose that $SIM is also defined in bash.rc.

I know that in shell, I can do something like this:
Expand|Select|Wrap|Line Numbers
  1. CKPT_DIR=`grep "^ckptdir" $HOME/a.tcl | sed -e "s/ckptdir"`
How can I do that in python?
Jun 11 '10 #1
2 1949
bvdet
2,851 Expert Mod 2GB
Maybe I'm just dense, but I cannot tell what you are trying to do. Maybe it's because I'm on Windows and not familiar with your operating system. If you can show me a sample file content and what you want to read from it, I would like to help.
Jun 13 '10 #2
I found it... I have to open the file ans search for that pattern. Something like this:
Expand|Select|Wrap|Line Numbers
  1. infile = open("my_text.txt","r")
  2. text = infile.read()
  3. infile.close()
  4.  
  5. search = "Fred"
  6. index = text.find(search)
  7. print search, "found at index", index
Jun 14 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Jean-Marie Delapierre | last post by:
Hi, I have two files in a directory. one called toto.txt with the following content : 1 2 3 4 5
2
by: Eunice | last post by:
Hi, I want to extract strings from the following text file content. ********************* Description length of global graph compressed using this substructure = 302.907315 Compression =...
0
by: Bubbles | last post by:
Hello. New to ASP.NET and struggling on this one. I have a text file with a bunch of text in it. Throughout the file words followed by a ":" will appear. I need to pull every such string out...
6
by: RSH | last post by:
Hi, I have quite a few .DAT data files that i need to extract the data out of. When i open the files in a text editor I see all of the text that I need to get at BUT there are a lot of junk...
16
by: Preben Randhol | last post by:
Hi A short newbie question. I would like to extract some values from a given text file directly into python variables. Can this be done simply by either standard library or other libraries? Some...
13
by: DH | last post by:
Hi, I'm trying to strip the html and other useless junk from a html page.. Id like to create something like an automated text editor, where it takes the keywords from a txt file and removes them...
3
by: gujarsachin2001 | last post by:
Hi friends, I m just stuck with extracting excel file data into text file. So can any body please help me out. thx, Sachin.
6
by: leegold | last post by:
say I have a text file: zz3 uaa4a ss 7 uu zz 3 zzzz66 ppazz9 aaaaaaaaa00000zz0 I want to sort the text file. I want the key to be the number after the two "zz". Or I guess a string of two...
4
by: dexter48 | last post by:
Hi I'm searching for a string occurance in a text file. I find the string ok and write the results to a log file. But on the line above is also some information I need. How can i get that. The string...
4
by: poolboi | last post by:
hi guys i've having some problem extracting data from a text file example if i got a text file with infos like: Date 2008-05-01 Time 22-10 Date 2008-05-01 Time 21-00 Date 2008-05-02 Time...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.