Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
173 views
in Technique[技术] by (71.8m points)

python - How do I read and replace specifc value in one column of the text files?

I have around 70000 text files in a folder that contain a table comprised of 5 columns e.g:

7   0.1   0.2     0.007    0.000077
7   0.6   0.8888  0.9      0.07
3   0.8   0.09999 0.87     0.6544444
1   0.0009 0.09   0.999    0.777777
6   0.2    0.333  0.222    0.33333

I'd like to replace the number "7" in the first column with the "5". To do this I wrote the following code to read the text files in the folder and changes 7 to 5 in the entire text file.

My question is how do I apply this to only column one instead of the entire text file?

import glob
for filepath in glob.iglob('path to folder/*.txt', recursive=True):
    with open(filepath) as file:
        s = file.read()
    s = s.replace('7', '5')
    with open(filepath, "w") as file:
        file.write(s)
    
question from:https://stackoverflow.com/questions/65559712/how-do-i-read-and-replace-specifc-value-in-one-column-of-the-text-files

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The fileinput module has a convenient inplace keyword argument.

from fileinput import input as fileinput
import glob

with fileinput(files=glob.iglob('path to folder/*.txt', recursive=True), inplace=True) as f:
    for line in f:
        fields = line.split('')
        if fields[0] == '7':
            line = ''.join(['5'] + fields[1:])
        print(line, end='')

Perhaps try without inplace=True until you have convinced yourself that this works correctly.

I have simply assumed your columns are tab-delimited; it should not be too hard to adapt this to space-delimited or to simply use a different delimiter like comma etc. (If your input is proper CSV, perhaps use Python's csv module instead.)

I have also assumed that you only want to change 7 to 5 and not also e.g. 777 to 555. If you wanted that, perhaps simply

    # ...
    fields[0] = fields[0].replace('7', '5')
    line = ''.join(fields)
    # ...

If your columns are separated by unpredictable amounts of whitespace but the field length does not change when you replace (like here, where you replace a single character with a single character), you can use

    fields = line.split()
    fields[0] = fields[0].replace('7', '5')
    line = fields[0] + line[len(fields[0]):]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...