When storing the acquired character string in an array while scraping Extra line breaks in the array ... At that time, I came across a scene where I wanted to replace or delete it. With the meaning of a memorandum
python
str_list = ['Ai\n Ueo', 'Kaikukeko', 'Sashi\n suse\n', 'Tachitsute\n and', 'What\n nu\n\n']
str_list2 = [['Ai\n Ueo', 'Kaikukeko'], ['Sashi\n suse\n', 'Tachitsute\n and', 'What\n nu\n\n'], ['Hahifu\n Heho', 'Ma\n Mimumemo\n'], ['yayuyo'], ['Lari\n Rurero', 'Won']]
If you scrape like this, it may contain characters other than the one you want
usually
replace_list = []
for s in str_list:
if '\n' in s:
text = s.replace('\n', '')
replace_list.append(text)
else:
replace_list.append(s)
print(replace_list)
>>['AIUEO', 'Kaikukeko', 'SA Shi Su Se So', 'TA Chi Tsu Te to', 'What is it']
for i in str_list2:
for s in i:
if '\n' in s:
text = s.replace('\n', '')
replace_list2.append(text)
else:
replace_list2.append(s)
replace_list3.append(replace_list2)
replace_list2 = []
print(replace_list3)
>>[['AIUEO', 'Kaikukeko'], ['SA Shi Su Se So', 'TA Chi Tsu Te to', 'What is it'], ['Hahifuheho', 'Mamimumemo'], ['yayuyo'], ['Larry Lero', 'Won']]
Comprehension notation
replace_list = [s.replace('\n', '') for s in str_list]
replace_list2 = [[s.replace('\n', '') for s in text] for text in str_list2]
print(replace_list)
print(replace_list2)
>>['AIUEO', 'Kaikukeko', 'SA Shi Su Se So', 'TA Chi Tsu Te to', 'What is it']
>>[['AIUEO', 'Kaikukeko'], ['SA Shi Su Se So', 'TA Chi Tsu Te to', 'What is it'], ['Hahifuheho', 'Mamimumemo'], ['yayuyo'], ['Larry Lero', 'Won']]
Which one is better just by using the inclusion notation
Recommended Posts