简体   繁体   中英

I have a txt file of 100 items. Each line starts with ", (digit)(digit).(digit)(digit) miles - " my goal is to remove these 16 characters on each line

Been stuck on this for a week!

Here are the lines of the.txt file I am trying to strip the prefixes of:

, 3.33 miles - 20314
, 4.3 miles - 21
, 5.64 miles - 432
, 5.81 miles - 5205
, 6.02 miles - 503
, 6.78 miles - 5125
, 7.2 miles - 423
, 7.5 miles - 2265
, 8.89 miles - 622

The goal is to use python to strip the beginning characters starting with the comma and ending with the empty space after the dash from each line.

If the number of characters from the end is always the same you can use the generator to edit the current list.

listT = [', 3.33 miles - 203',', 4.3 miles - 215',', 5.64 miles - 432',', 5.81 miles - 520',', 6.02 miles - 503',', 6.78 miles - 512',', 7.2 miles - 423',', 7.5 miles - 226',', 8.89 miles - 622']

listT = [name[-3:] for name in listT ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM