-1

Imagine you have a textfile input.txt containing text and floats, but without a regular structure (such as header, .csv etc.), for instance :

Banana 1.4030391
(4.245, -345.2456)
4.245 -345.2456
Hello how are you?

Based on this file, you want to generate output.txt where each float has been rounded to 1 decimal, the remaining content left untouched. This would give

Banana 1.4
(4.2, -345.2)
4.2 -345.2
Hello how are you?

To achieve this in Python, you need following steps.

  1. Open the inputfile and read each line

f = open('input.txt') f.readlines()

  1. Extract the floats

How to proceed? The difficulty lies in the fact that there is no regular structure in the file.

  1. Round the floats

np.round(myfloat)

  1. Write the line to the output file

...

Karlo
  • 1,630
  • 4
  • 33
  • 57
  • 1
    What have you tried so far? –  Aug 06 '21 at 02:12
  • Only what I've mentioned in the question. I could not find information about how to extract the floats, only about the case where each line has the same structure, for instance `(1.2, 1.3, 1.5)` which makes extracting the floats very easy. – Karlo Aug 06 '21 at 02:16
  • 2
    A regular expression could extract the floats quite easily, and also give you the non-float characters in between them at the same time. – Mark Ransom Aug 06 '21 at 02:34

2 Answers2

1

Check this out. Use regular expression to match floating point numbers, then replace them.

import re
f = open('input.txt') 
tempstring=f.readlines()

string = ""
string = string.join(tempstring)
def check_string(string):

    temp = re.findall(r"\d+\.\d+",string)
    for i in temp:
        string=string.replace(i,str(round(float(i),1)))
    return string

output=check_string(string)
file2=open("output.txt","a+")
file2.write(output)
Karlo
  • 1,630
  • 4
  • 33
  • 57
  • There seems to be a minor error: `re.findall(...)` raises `TypeError: expected string or bytes-like object`. When substituting `string` manually by `"(4.245, -345.2456)"`, it works fine! So the problem lies with the type of `string=f.readlines()`. – Karlo Aug 06 '21 at 03:00
  • I have modified your code: `f.readlines()` returns a list, that has to be converted into a string first. – Karlo Aug 06 '21 at 03:12
  • 1
    @Karlo No problem. Happy to hear it solves the problem ```:-)``` –  Aug 06 '21 at 03:13
  • Thank you! (I am just leaning about how to import a text file, so this code is very interesting.) – Karlo Aug 06 '21 at 03:14
1

Since it seems like you need ideas how to extracts floats from the text file, I can only contribute an idea. I think it is simpler to create an empty list and add each words and numbers to it. You can strip each items in the text file by stripping it where there is a space and newline. Then you can check if those items in list are floats, by using for loop. Functions you can use are ".append", ".rstrip", "isinstance()" Below code DOESN'T extract float numbers but you can work on it to strip each items in text file.

mylines = []                                # Declare an empty list.
with open ('text.txt', 'rt') as myfile:    # Open txt for reading text.
    for myline in myfile:                   # For each line in the file,
        mylines.append(myline.rstrip('\n' and ' ')) # strip newline and add to list.
for element in mylines:  
    print(element)
    for item in element:
            print(item)
Amur Koko
  • 11
  • 3