W
W
werastet2021-09-05 20:26:33
Python
werastet, 2021-09-05 20:26:33

File cleaner after sign?

Good evening guys.
I need a script that will clear the file after a certain character, for example, I have
164672jeirjfueheh: mom don Bob
Uehehe72+7388: udiend jejd jejaj
and I need only
164672jeirjfueheh
Uehehe72+7388
(That is, everything after ":" It is necessary to delete including " :"
File in array and lines over 1 billion

Answer the question

In order to leave comments, you need to log in

4 answer(s)
D
dmshar, 2021-09-05
@dmshar

Good evening guys.
- hi, kid.
I'm trying to translate what is written in the question into normal language.
"File in array" - i.e. the data has already been read into the array. Then where does the "file cleaner"?
"a script that will clear in the file after a certain character" - Contradicts the further example, in which it is not actually the "file" that is cleared, but simply deleted in each file record (or rather, in each array element) what comes after ":" .
"rows more than 1 billion" - not impressive.
So, if the data is already in the "array" - however, it is not clear how you organized it in Python, but we will assume that it is still using a list - then everything is elementary:

arr =['164672jeirjfueheh: mom don Bob','Uehehe72+7388: udiend jejd jejaj']
for i,row in enumerate(arr):
    arr[i]=row.split(':')[0]
print (arr)

Result: You can, if necessary, return to a file. In the future, try to state your question more clearly and technically competently.
['164672jeirjfueheh', 'Uehehe72+7388']

G
galaxy, 2021-09-05
@galaxy

cut -d: -f1 file.txt > filtered.txt

V
Vladimir Kuts, 2021-09-05
@fox_12

Clue:

'164672jeirjfueheh: mom don Bob'.split(':')[0]
# '164672jeirjfueheh'

Run through the entire file and perform this simple operation on each line

A
Alexander, 2021-09-05
@shabelski89

class FileHandler:
    def __init__(self, filename):
        self.filename = filename

    def read(self):
        with open(self.filename, 'r', encoding='utf-8') as in_file, \
                open(f'result_{self.filename}', 'w', encoding='utf-8') as out_file:
            for line in in_file:
                if line:
                    out_line = self.__parse(line)
                    out_file.write(out_line)
                    out_file.write("\n")

    @staticmethod
    def __parse(line):
        result, *_ = line.split(":")
        return result


if __name__ == "__main__":
    f = FileHandler(r'yot_file_name')
    f.read()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question