Viewed 41k times. The code in your question is not the code generating the results shown. Add a comment. Active Oldest Votes. Matt-Mac-Muffin: Indeed! Boris Boris 9, 7 7 gold badges 72 72 silver badges 74 74 bronze badges. This incorporates the strip directly into the for statement. Chad Skeeters Chad Skeeters 1, 16 16 silver badges 18 18 bronze badges.
Using map isn't really necessary. You can use a generator expression and do it like this for line in line. I think this is the best option. Top 3 video Explaining python - How to read a file without newlines? Does Python have a ternary conditional operator? Does Python have a string 'contains' substring method? If the file contains many lines, it removes the information, where each line ended. YScharf YScharf 3 3 silver badges 7 7 bronze badges.
Necriss Necriss 11 1 1 bronze badge. Please add some explanation so that it will be useful to others. You should use a context manager to handle the file object, and iterate over the file directly. By using. But what if the line has a comma in it? Pang 8, gold badges 82 82 silver badges bronze badges.
The Overflow Blog. Podcast Making Agile work for data science. Stack Gives Back Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually.
Linked See more linked questions. Related Hot Network Questions. Question feed. Stack Overflow works best with JavaScript enabled. Accept all cookies Customize settings. Connect and share knowledge within a single location that is structured and easy to search. I'm trying to remove all newline characters from a string.
I've read up on how to do it, but it seems that I for some reason am unable to do so. Here is step by step what I am doing:. And I'm still seeing the newline character in the output. I've tried with rstrip as well, but I'm still seeing the newline.
Could anyone shed some light on why I'm doing this wrong? You want to use replace :. Answering late since I recently had the same question when reading text from file; tried several options such as:.
In your case, you may want to try replace :. Try replace. If the file includes a line break in the middle of the text neither strip nor rstrip will not solve the problem,. Stack Overflow for Teams — Collaborate and share knowledge with a private group.
0コメント