Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python: find and replace numbers < 1 in text file

Tags:

python

replace

I'm pretty new to Python programming and would appreciate some help to a problem I have...

Basically I have multiple text files which contain velocity values as such:

0.259515E+03 0.235095E+03 0.208262E+03 0.230223E+03 0.267333E+03 0.217889E+03 0.156233E+03 0.144876E+03 0.136187E+03 0.137865E+00

etc for many lines...

What I need to do is convert all the values in the text file that are less than 1 (e.g. 0.137865E+00 above) to an arbitrary value of 0.100000E+01. While it seems pretty simple to replace specific values with the 'replace()' method and a while loop, how do you do this if you want to replace a range?

thanks

like image 256
hjp Avatar asked Apr 21 '10 17:04

hjp


4 Answers

I think when you are beginning programming, it's useful to see some examples; and I assume you've tried this problem on your own first!

Here is a break-down of how you could approach this:

contents='0.259515E+03 0.235095E+03 0.208262E+03 0.230223E+03 0.267333E+03 0.217889E+03 0.156233E+03 0.144876E+03 0.136187E+03 0.137865E+00'

The split method works on strings. It returns a list of strings. By default, it splits on whitespace:

string_numbers=contents.split()
print(string_numbers)
# ['0.259515E+03', '0.235095E+03', '0.208262E+03', '0.230223E+03', '0.267333E+03', '0.217889E+03', '0.156233E+03', '0.144876E+03', '0.136187E+03', '0.137865E+00']

The map command applies its first argument (the function float) to each of the elements of its second argument (the list string_numbers). The float function converts each string into a floating-point object.

float_numbers=map(float,string_numbers)
print(float_numbers)
# [259.51499999999999, 235.095, 208.262, 230.22300000000001, 267.33300000000003, 217.88900000000001, 156.233, 144.876, 136.18700000000001, 0.13786499999999999]

You can use a list comprehension to process the list, converting numbers less than 1 into the number 1. The conditional expression (1 if num<1 else num) equals 1 when num is less than 1, otherwise, it equals num.

processed_numbers=[(1 if num<1 else num) for num in float_numbers]
print(processed_numbers)
# [259.51499999999999, 235.095, 208.262, 230.22300000000001, 267.33300000000003, 217.88900000000001, 156.233, 144.876, 136.18700000000001, 1]

This is the same thing, all in one line:

processed_numbers=[(1 if num<1 else num) for num in map(float,contents.split())]

To generate a string out of the elements of processed_numbers, you could use the str.join method:

comma_separated_string=', '.join(map(str,processed_numbers))
# '259.515, 235.095, 208.262, 230.223, 267.333, 217.889, 156.233, 144.876, 136.187, 1'
like image 60
unutbu Avatar answered Nov 02 '22 21:11

unutbu


typical technique would be:

  • read file line by line
  • split each line into a list of strings
  • convert each string to the float
  • compare converted value with 1
  • replace when needed
  • write back to the new file

As I don't see you having any code yet, I hope that this would be a good start

like image 26
SilentGhost Avatar answered Nov 02 '22 20:11

SilentGhost


def float_filter(input):
    for number in input.split():
        if float(number) < 1.0:
            yield "0.100000E+01"
        else:
            yield number

input = "0.259515E+03 0.235095E+03 0.208262E+03 0.230223E+03 0.267333E+03 0.217889E+03 0.156233E+03 0.144876E+03 0.136187E+03 0.137865E+00"
print " ".join(float_filter(input))
like image 44
ephes Avatar answered Nov 02 '22 21:11

ephes


import numpy as np

a = np.genfromtxt('file.txt')  # read file
a[a<1] = 0.1                   # replace
np.savetxt('converted.txt', a) # save to file
like image 41
jfs Avatar answered Nov 02 '22 20:11

jfs