473,408 Members | 2,405 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,408 software developers and data experts.

how to discard a line if it's not a number?

Hi, I'm reading a file line by line, and whenever a line is not
consisted of a single number (such as 0.315), I want to discard that
line (and store only the single numbers).

For example,

0.315
discarded this line of text
3.8
-1.44
forget about me also
2.6
Then I want to store only the four numbers into another file, without
the two sentences.
Suggestions are appreciated!

Oct 29 '05 #1
1 1510
This is a possible solution, using exceptions:

fileName = "data"
out = file(fileName + "_filt.txt", "w")
for line in file(fileName + ".txt"):
try:
nline = float(line)
except ValueError:
pass
else:
out.write(str(nline) + "\n")
out.close()

If the file is small enough this can be a little faster:

fileName = "data"
filtered = []
data = file(fileName + ".txt").readlines()
for line in data:
try:
filtered.append( str(float(line)) )
except ValueError:
pass
out = open(fileName + "_filt.txt", "w")
out.write( "\n".join(filtered) )
out.close()

Bye,
bearophile

Oct 29 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jason Gyetko | last post by:
I need to delete several million records from a single table but cannot get DB2 8.1 to not create log files (I need to do this as fast as possible to minimize downtime). I figure that creating the...
1
by: vishal | last post by:
What does #line 182 parser.y mean in a C program?
5
by: kutty | last post by:
Hi All, I am loading data to a child table from a text file. the text files also contains data not referenced by parent key. while loading the data if one row fails to satisfies the constraint...
3
by: valuedyeng | last post by:
Basically I am trying to run a check through a field which allow the use to input their name... However I want to make sure they only entered alphabets rather than number Can anyone help me out for...
2
by: Jim | last post by:
Hi, I'm trying to sort a vector using sort(corelist.begin(),corelist.end()); so I've provided a method to overload the < operator in the file, like so, but it isn't a class member and the code...
11
by: Horacius ReX | last post by:
Hi, I have to search for a string on a big file. Once this string is found, I would need to get the number of the line in which the string is located on the file. Do you know how if this is...
3
by: jacob600 | last post by:
Hello, I am very new to Perl. I have been trying to modify an existing script that is used to read tcpdump files and then eventually generate Top Talker stats from it. If my tcpdump file from...
12
by: loial | last post by:
I have a requirement to compare 2 text files and write to a 3rd file only those lines that appear in the 2nd file but not in the 1st file. Rather than re-invent the wheel I am wondering if anyone...
10
by: trochia | last post by:
I hope I am able to describe this properly, but I inherited a form that is js and passes to a php mail() function. I'm pretty new to javascript also. I usually do all my forms in php tossing into...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.