David Y.
—I have a text file (dna.txt
) containing multiple lines, for example:
ATCAGTGGAAACCCAGTGCTA GAGGATGGAATGACCTTAAAT CAGGGACGATATTAAACGGAA
Using Python, how do I read it into a string variable as one long line, i.e. removing newlines? I want the final string to look like this:
ATCAGTGGAAACCCAGTGCTAGAGGATGGAATGACCTTAAATCAGGGACGATATTAAACGGAA
We can achieve this using the following Python code:
with open("dna.txt", "r") as file: dna = file.read().replace("\n", "") print(dna) # will print ATCAGTGGAAACCCAGTGCTAGAGGATGGAATGACCTTAAATCAGGGACGATATTAAACGGAA
In the above code:
open("dna.txt", "r")
opens the file in read mode (r
). We use Python’s with
statement to automatically close the file at the end of the block.file.read()
reads the entire contents of the file into a string.replace("\n", "")
is a string method that replaces all newline characters in our string with empty strings.In some cases, we may prefer to replace newlines with other characters, such as a single space. We can do this with a slight modification to the above code:
with open("dna.txt", "r") as file: dna = file.read().replace("\n", " ") # replace newline with space print(dna) # will print ATCAGTGGAAACCCAGTGCTA GAGGATGGAATGACCTTAAAT CAGGGACGATATTAAACGGAA
An alternative but less explicit way to produce the same output would be to use str.splitlines
and str.join
. This will create a list containing each line in the file, and then convert that list into a string with a specified delimiter. We can use an empty string to remove the new lines entirely:
with open("dna.txt", "r") as file: dna = "".join(file.read().splitlines()) print(dna) # will print ATCAGTGGAAACCCAGTGCTAGAGGATGGAATGACCTTAAATCAGGGACGATATTAAACGGAA
Alternatively, we could use any other string to separate the lines with that string:
with open("dna.txt", "r") as file: dna = " ".join(file.read().splitlines()) # separate lines with a single space print(dna) # will print ATCAGTGGAAACCCAGTGCTA GAGGATGGAATGACCTTAAAT CAGGGACGATATTAAACGGAA
While both of these approaches produce the same output, the second one may be confusing to readers unfamiliar with Python.
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at compliance@sentry.io.
If you are a California resident, see our Supplemental notice.