Answer the question
In order to leave comments, you need to log in
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
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)
['164672jeirjfueheh', 'Uehehe72+7388']
Clue:
'164672jeirjfueheh: mom don Bob'.split(':')[0]
# '164672jeirjfueheh'
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 questionAsk a Question
731 491 924 answers to any question