perl2python: Reading files
Table of Contents
File
Read files
• perl
#!/usr/bin/perl -w
use strict;
open FILE, "data/test.fa" or die "$!";
while(my $line = <FILE> ){
print "$line";
}
use strict;
open FILE, "data/test.fa" or die "$!";
while(my $line = <FILE> ){
print "$line";
}
## >gene1
## ATGGCTAGCATCGTACGTCG
## >gene2
## ATGGCTAGCTACGTACGTAG
## ATGGCTAGCATCGTACGTCG
## >gene2
## ATGGCTAGCTACGTACGTAG
• python
infile = open("data/test.fa", 'r')
for line in infile.readlines():
line = line.rstrip('\n')
print line
for line in infile.readlines():
line = line.rstrip('\n')
print line
## >gene1
## ATGGCTAGCATCGTACGTCG
## >gene2
## ATGGCTAGCTACGTACGTAG
## ATGGCTAGCATCGTACGTCG
## >gene2
## ATGGCTAGCTACGTACGTAG
Here readline() reads one line character at a time, readlines() reads in the whole file at once and splits it by line.
The xreadlines() function should be used for big files:
infile = open("data/test.fa", 'r')
for line in infile.xreadlines():
line = line.rstrip('\n')
print line
for line in infile.xreadlines():
line = line.rstrip('\n')
print line
## >gene1
## ATGGCTAGCATCGTACGTCG
## >gene2
## ATGGCTAGCTACGTACGTAG
## ATGGCTAGCATCGTACGTCG
## >gene2
## ATGGCTAGCTACGTACGTAG
HERE documents
• Perl code
$len_gene1 = 100;
$len_gene2 = 200;
print <<OUT;
gene1\t$len_gene1
gene2\t$len_gene2
OUT
$len_gene2 = 200;
print <<OUT;
gene1\t$len_gene1
gene2\t$len_gene2
OUT
## gene1 100
## gene2 200
## gene2 200
• Python code
len_gene1 = 100
len_gene2 = 200
print '''
gene1\t%s
gene2\t%s
'''[1:-1] % (len_gene1, len_gene2)
len_gene2 = 200
print '''
gene1\t%s
gene2\t%s
'''[1:-1] % (len_gene1, len_gene2)
## gene1 100
## gene2 200
## gene2 200
Or so long as there's no indentation on the first line or trailing space on the last:
print '''
dog
cat
'''.strip()
dog
cat
'''.strip()
## dog
## cat
## cat
Comments
Post a Comment