Here is my string:
FROM nginx:stable
COPY TestDataGenerator/out /usr/share/nginx/html
COPY MockServer/nginx.conf /etc/nginx/nginx.conf
# comment
RUN chmod g+rwx /var/cache/nginx /var/run /var/log/nginx
EXPOSE 8080
Here is my code to remove newlines (only newlines as separate list elements, i don't care about linebreaks after string in a line) and comments (items containing #):
content = '''FROM nginx:stable
COPY TestDataGenerator/out /usr/share/nginx/html
COPY MockServer/nginx.conf /etc/nginx/nginx.conf
# comment
RUN chmod g+rwx /var/cache/nginx /var/run /var/log/nginx
EXPOSE 8080
'''
list = content.splitlines()
for item in list:
if item == '' or item.__contains__('#'):
list.remove(item)
print(list)
Here is the result:
['FROM nginx:stable', 'COPY TestDataGenerator/out /usr/share/nginx/html', 'COPY MockServer/nginx.conf /etc/nginx/nginx.conf', '', '', 'RUN chmod g+rwx /var/cache/nginx /var/run /var/log/nginx', 'EXPOSE 8080']
Out of 4 separate newlines 2 got removed. How can I remove all 4 newlines and why does it behave this way?