J
J
Jlokys2020-06-14 11:29:53
Python
Jlokys, 2020-06-14 11:29:53

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

5 answer(s)
S
soremix, 2020-06-14
@Jlokys

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)

D
Dr. Bacon, 2020-06-14
@bacon

You have already been told everything How to delete a line with 20 characters in python?

T
Timur Pokrovsky, 2020-06-14
@Makaroshka007

Find out the quantity; in a string using the str.count(';') function. What else does?

D
diezeitschriftenliteratur, 2020-06-14
@diezeitschriftenliteratur

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 символ новой строки

L
Leon Yarovinsky, 2014-08-05
@leonyarovinski

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 question

Ask a Question

731 491 924 answers to any question