Answer the question
In order to leave comments, you need to log in
How to remove short lines from a file?
The file has lines completely written and there is no way to write a program so that short lines are deleted. It is desirable that it be determined by the symbol ( ; ) if there are less than 6 of these characters in the line, then delete it or not write it. I don’t know how to write it in python, help, Thank you)
Here is an example;
93550;Ulan-Ude;02;6;12; on; 0; - - ;ets ;% ;0000 ;r ;1150; 80; ?/? ;ON ;
93550;Ulan-Ude;02;6;4;to; 0 ;- - ;ec ;%; 0000 ;p ;1150 ;80;?/? ;ON ;
93550;Ulan-Ude;02;6
94010;Petrovsky Plant;02;13; 4 ;by; 0 - -; ets; %;6630;p;776;51;?/?; ON ;
94010;Petrovsky Plant;02;13; 6; on; 0 - -; ec ;% ;6630; R; 752;49; ?/?; ON ;
94010;Petrovsky Plant;02;13
94020;Dekabrista Park;01;15; 4;by; 0; - - ;ets;% ;6630; p ;1160; 79 ;?/?; ON ;
94020;Park Decembrists;01;15
Answer the question
In order to leave comments, you need to log in
with open('myfile.txt', 'r', encoding='utf-8') as f:
lines = f.readlines()
new = [line for line in lines if line.count(';') >= 6]
print(new)
You have already been told everything How to delete a line with 20 characters in python?
Find out the quantity; in a string using the str.count(';') function. What else does?
If you need to write lines from some array (let's say with the list type) to the file with the condition that the character ; will be repeated 6 or more times then use this code:
info = [
"info;info;info;info;info;info;info;info",
"info;info;info",
"info;info;info;info;info;info;info;info;info;info;info"
"info;info;info;info",
"info;info;info;info;info;info;info;info;info;info;info;info;info;info;info"
]
with open("file", "wt") as fl:
for i in info:
if i.count(";") >= 6:
fl.write(i + "\n") # \n escape символ новой строки
I don’t know, but when I used a war thunder laptop on a weak Wi-Fi (one stick at most), then when I turned on the game, the Internet crashed, and of course it didn’t let me enter the hangar, etc.
Bottom line: Maybe the connection is weak?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question