473,396 Members | 1,970 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,396 software developers and data experts.

searching for character in file

31
i'm given an open file, and basically they give you a character(a str) and whenever the occurence of that string appears as the starting word of the line, we are to return that line.

as an example:
the file =

fds dfds kjkj kjkdsf
abc def hij
klm nop qrs
tuv wx yz

and the character given is "klm"

i am to return the line "klm nop qrs"

how would i be able to write this??!!
Mar 9 '09 #1
5 1563
v13tn1g
31
this is what i have so far

Expand|Select|Wrap|Line Numbers
  1. def lines_start_with(f, a):
  2.     for line in f:
  3.         if line.startswith(a):
  4.             print line.strip()
  5.  
the thing is that when i test it it doesn't print out anything :S
Mar 9 '09 #2
boxfish
469 Expert 256MB
That function works fine for me. Maybe you could post your test program.
Mar 9 '09 #3
v13tn1g
31
file_name("file url" , "character")

thats how i tested it
Mar 9 '09 #4
Smygis
126 100+
Expand|Select|Wrap|Line Numbers
  1. >>> l=['fds dfds kjkj kjkdsf', 'abc def hij', 'klm nop qrs', 'tuv wx yz']
  2. >>> for i in l:
  3. ...     if "klm" in i:
  4. ...             print i
  5. ... 
  6. klm nop qrs
  7. >>> 
  8.  
Mar 9 '09 #5
boxfish
469 Expert 256MB
You have to pass your function a file object, not the file name. Try something like this:
Expand|Select|Wrap|Line Numbers
  1. my_file = open("my_file.txt")
  2. lines_start_with(my_file, "klm")
I hope this works.
Mar 9 '09 #6

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

Similar topics

18
by: jblazi | last post by:
I should like to search certain characters in a string and when they are found, I want to replace other characters in other strings that are at the same position (for a very simply mastermind game)...
1
by: Adrian | last post by:
This script works well for searching thru a SELECT list but doesnt work so well with the MULTIPLE option. I need to modify it to work for a SELECT MULTIPLE but I have no idea where to begin, any...
5
by: Ada | last post by:
hello folks, just wanna get some feedback from someone here who has more experience. :-) i'm developing a small app that require searching for the highest index within a directory. i was...
17
by: AL | last post by:
Hi I have a bunch of text files which need to be organized. What I want to do is to select three continuous lines from each text file and write them down in another text file. Each of those three...
7
by: pbd22 | last post by:
Hi. I am somewhat new to this and would like some advice. I want to search my xml file using "keyword" search and return results based on "proximity matching" - in other words, since the search...
21
by: Umesh | last post by:
/*program to search a* in a text file & write output in a file.* indicated any character*/ #include<stdio.h> #include<stdlib.h> int main(void) { FILE *f,*fp; f=fopen("c:/1.txt","r");...
3
by: Ahmad Jalil Qarshi | last post by:
Hi, I have a text file having size about 2 GB. The text file format is like: Numeric valueAlphaNumeric values Numeric valueAlphaNumeric values Numeric valueAlphaNumeric values For example...
5
by: praveenhathwar | last post by:
Hi, Im new to this Forum. Im facing a problem from long time. I have a text box. If user types a character, I should display the itemname (records/data) into Flexgrid starting from those characters....
12
by: Alexnb | last post by:
This is similar to my last post, but a little different. Here is what I would like to do. Lets say I have a text file. The contents look like this, only there is A LOT of the same thing. () A...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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.