Python removes all spaces from entries in a list

when calling readlines() in a .srt file, I got a list of characters with a lot of leading and trailing spaces, as shown below

 with open(infile) as f: r=f.readlines() return r 

I got this list

 ['\xef\xbb\xbf1\r\n', '00:00:00,000 --> 00:00:03,000\r\n', "[D. Evans] Now that you've written your first Python program,\r\n",'\r\n', '2\r\n', '00:00:03,000 --> 00:00:06,000\r\n', 'you might be wondering why we need to invent new languages like Python\r\n', '\r\n'] 

I just included a few elements for brevity. How to clear this list so that I can remove all whitespace characters and get only the relevant elements, such as

  ['1','00:00:00,000 --> 00:00:03,000',"[D. Evans] Now that you've written your first Python program"...] 
+4
source share
1 answer

You can split each line. Running it as a generator can also save you some memory if you work with a large file.

Also, it looks like you are working on a UTF-8 file with a specification (which is something silly or at least unnecessary) for the first few characters, so you need to open it differently.

 import codecs def strip_it_good(file): with codecs.open(file, "r", "utf-8-sig") as f: for line in f: yield line.strip() 
+11
source

Source: https://habr.com/ru/post/1413216/


All Articles