Hi I am having problem with print the output in a text file.
Suppose, I have an array
A=[ [1,2,3],
[4,5,6],
[7,8,9] ]
I have the code :
for i in A:
for j in i:
print(j),
print ""
it will print
1 2 3
4 5 6
7 8 9
now I have the code to print the same output in a text file
for i in A:
for j in i:
print >> file,j
print(j),
print ""
print >> file,""
but this is not writing the same previous console output in the file. How can I write the same output ??
Thanks.
Could you try the below code?
It should work as you want it to
A=[ [1,2,3],
[4,5,6],
[7,8,9] ]
f = open('myfile','w')
for i in A:
for j in i:
print(j),
f.write(str(j) + ' ')
f.write("\n")
f.close()
Related
I have some problem for using Biopython, count and sum each base's numbers for parsing FASTA file. In FASTA file, total A is how much? and total T is?
but there's some problem.
1.
handle2="/home/koreanraichu/sra_data_mo.fasta"
for record2 in SeqIO.parse(handle2,"fasta"):
print(Seq(record2.seq).count("A"))
print(type(Seq(record2.seq).count("A")))
This is code, was it successfully read sequence and count adenine, but It never summarize each numbers. I tried it for list append and sum(), simply add but there's no effective. (each count type is int, but never added and printed separately)
for record2 in SeqIO.parse(handle2,"fasta"):
if len(record2.seq) > 100:
i=0
i=i+len(record2.seq)
else:
j=0
j=j+len(record2.seq)
print(i,j)
like upper, this code doesn't work. I meant this code for It is a conditional sum code that adds DNA of 100 bp or more and DNA of less than 100 bp separately. but it never works, too. it prints last record's data.
What can I do things for solve this?
try this code for first problem:
from Bio import SeqIO
# from Bio.Seq import Seq
handle2="Fasta.fa"
for record2 in SeqIO.parse(handle2,"fasta"):
# print(record2.seq, type(record2.seq))
# print(str(record2.seq), type(str(record2.seq)))
print(record2.seq.count("A"))
# print(type(record2.seq).count("A")) ### --> TypeError: count() missing 1 required positional argument: 'sub'
summarize = 0
for i in 'ATGC':
x = record2.seq.count(i)
print(i, ' : ', x)
summarize += record2.seq.count(i)
print(summarize)
given my test fasta :
>Rosalind_4402
GCAGCTAGCTAGCTAGCTGGGATTCGGATCGGCGCCCCGAGAGGATTCTTTCAGCTGTAA
GAATTTATCCTCGATCGGGCTATAAAACCTACGCATATCTGCTAGCTGAGGGGCTATCTT
output:
27
A : 27
T : 32
G : 32
C : 29
120
second code :
from Bio import SeqIO
# from Bio.Seq import Seq
# handle2="/home/koreanraichu/sra_data_mo.fasta"
handle2="Fasta2.fa"
i=0
j=0
for record2 in SeqIO.parse(handle2,"fasta"):
if len(record2.seq) > 100:
print('>100 : ', len(record2.seq))
i=i+len(record2.seq)
else:
print('else : ', len(record2.seq))
j=j+len(record2.seq)
print('> 100 summarize : ', i, ' else summarize : ',j)
given test fasta:
>Rosalind_4402
GCAGCTAGCTAGCTAGCTGGGATTCGGATCGGCGCCCCGAGAGGATTCTTTCAGCTGTAA
GAATTTATCCTCGATCGGGCTATAAAACCTACGCATATCTGCTAGCTGAGGGGCTATCTT
>Rosalind_4403
GCAGCTAGCTAGCTAGCTGGGATTCGGATCGGCGCCCCGAGAGGATTCTTTCAGCTGTAA
GAATTTATCCTCGATCGGGCTATAAAACCTACGCATATCTGCTAGCTGAGGGGCTATCTT
GCAGCTAGCTAGCTAGCTGGGATTCGGATCGGCGCCCCGAGAGGATTCTTTCAGCTGTAA
GAATTTATCCTCGATCGGGCTATAAAACCTACGCATATCTGCTAGCTGAGGGGCTATCTT
>Rosalind_4404
GCAGCTAGCTAGCTAGCTGGGATTCGGATCGGCGCCCCGAGAGGATTCTTTCAGCTGTAA
>Rosalind_4405
GCAGCTAGCTAGCTAGCTGGGATTCGGATCGGCGCCCCGAGAGGATT
>Rosalind_4406
GCAGCTAGCTAGCTAGCTGGGATTCGGATCGGCGCCCCGAGAGGATTCTTTCAGCTGTAA
GAATTTATCCTCGATCGGGCTATAAAACCTACGCATATCTGCTAGCTGAGGGGCTATCTT
CTTTCAGCTGTAAGAATTTATCCTCGATCGGGCTATAAAACCTACGCATATCTGCTAGCT
GAGGGGCTATCTT
>Rosalind_4407
GCAGCTAGCTAGCTAGCTGGGATT
>Rosalind_4408
GCAGCTAGCTAGCTAGCTGGGATTCGGATCGGCGCCCCGAGAGGATTCTTTCAGCTGTAA
GAATTTATCCTCGATCGGGCTATAAAACCTACGCATATCTGCTAGCTGAGGGGCTATCTT
CTTTCAGCTGTAAGAATTTATCCTCGATCGGGCTATAAAACCTACGCATATCTGCTAGC
output:
>100 : 120
>100 : 240
else : 60
else : 47
>100 : 193
else : 24
>100 : 179
> 100 summarize : 732 else summarize : 131
Say I have the multi-lines text:
str = [[
The lazy dog sleeping on the yard.
While a lazy old man smoking.
The yard never green again.
]]
I can split each words using:
for w in str:gmatch("%S+") do print(w) end
But how I can get results as an example:
The = 3 words, line 1,3
Lazy = 2 words, line 1,2
Dog = 1 word, line 1
..and so on?
Thank you
You could detect the \n using gmatch like you are already to count the words.
The pattern would be something like "[^\n]+" and the code something like this:
local str = [[
The lazy dog sleeping on the yard.
While a lazy old man smoking.
The yard never green again.
]]
local words = {}
local lines = {}
local line_count = 0
for l in str:gmatch("[^\n]+") do
line_count = line_count + 1
for w in l:gmatch("[^%s%p]+") do
w = w:lower()
words[w] = words[w] and words[w] + 1 or 1
lines[w] = lines[w] or {}
if lines[w][#lines[w]] ~= line_count then
lines[w][#lines[w] + 1] = line_count
end
end
end
for w, count in pairs(words) do
local the_lines = ""
for _,line in ipairs(lines[w]) do
the_lines = the_lines .. line .. ','
end
--The = 3 words, line 1,3
print(w .." = " .. count .. " words , lines " .. the_lines)
end
Full output, note i also changed the pattern you used for capturing the words to "[^%s%p]+" i did this to remove the . that was getting attached to smoking, again, and yard.
smoking = 1 words , lines 2,
while = 1 words , lines 2,
green = 1 words , lines 3,
never = 1 words , lines 3,
on = 1 words , lines 1,
lazy = 2 words , lines 1,2,
the = 3 words , lines 1,3,
again = 1 words , lines 3,
man = 1 words , lines 2,
yard = 2 words , lines 1,3,
dog = 1 words , lines 1,
old = 1 words , lines 2,
a = 1 words , lines 2,
sleeping = 1 words , lines 1,
I have a file text "a.txt" :
1 2 3
4 5 6
7 8 9
Now i want store it in array 2d :
array ={ {1,2,3}{4,5,6}{7,8,9} }
I have try to :
array ={}
file = io.open("a.txt","r")
io.input(file)
i=0
for line in io.lines() do
array[i]=line
i=i+1
end
But it doesn't success.
Does anyone suggest me a way to do it?
You have some errors in your code. You first open the file a.txt and then set it for standard input. You don't need the open(). But i recommend to open the file and operate on it, using the lines() iterator on the file:
array = {}
file = io.open("a.txt","r")
i = 0
for line in file:lines() do
array[i]=line
i=i+1
end
Furthermore, with your method, you don't get the array you wished for ({ {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }) but instead an array containing strings as elements:
{ "1 2 3", "4 5 6", "7 8 9" }.
To get the latter, you have to parse the string you have read. An easy way to do this is to use string.match with captures:
array ={}
file = io.open("a.txt","r")
for line in file:lines() do
-- extract exactly three integers:
local t = { string.match(line, "(%d+) (%d+) (%d+)")) }
table.insert(array, t) -- append row
end
See https://www.lua.org/manual/5.3/manual.html#pdf-string.match. For a arbitrary number of integers (or other numbers) on every line, you can use a loop together with string.gmatch():
array ={}
file = io.open("a.txt","r")
for line in file:lines() do
local t = {}
for num in string.gmatch(line, "(%d+)") do
table.insert(t, num)
end
table.insert(array, t)
end
Hello experienced pythoners.
The goal is simply to read in my own files which have the following format, and to then apply mathematical operations to these values and polynomials. The files have the following format:
m1:=10:
m2:=30:
Z1:=1:
Z2:=-1:
...
Some very similar variables, next come the laguerre polynomials
...
F:= (12.58295)*L(0,x)*L(1,y)*L(6,z) + (30.19372)*L(0,x)*L(2,y)*L(2,z) - ...:
Where L stands for a laguerre polynomial and takes two arguments.
I have written a procedure in Python which splits apart each line into a left and right hand side split using the "=" character as a divider. The format of these files is always the same, but the number of laguerre polynomials in F can vary.
import re
linestring = open("file.txt", "r").read()
linestring = re.sub("\n\n","\n",str(linestring))
linestring = re.sub(",\n",",",linestring)
linestring = re.sub("\\+\n","+",linestring)
linestring = re.sub(":=\n",":=",linestring)
linestring = re.sub(":\n","\n",linestring)
linestring = re.sub(":","",linestring)
LINES = linestring.split("\n")
for LINE in LINES:
LINE = re.sub(" ","",LINE)
print "LINE=", LINE
if len(LINE) <=0:
next
PAIR = LINE.split("=")
print "PAIR=", PAIR
LHS = PAIR[0]
RHS = PAIR[1]
print "LHS=", LHS
print "RHS=", RHS
The first re.sub block just deals with formatting the file and discarding characters that python will not be able to process; then a loop is performed to print 4 things, LINE, PAIR, LHS and RHS, and it does this nicely. using the example file from above the procedure will print the following:
LINE= m1=1
PAIR= ['m1', '1']
LHS= m1
RHS= 1
LINE= m2=1
PAIR= ['m2', '1']
LHS= m2
RHS= 1
LINE= Z1=-1
PAIR= ['Z1', '-1']
LHS= Z1
RHS= -1
LINE= Z2=-1
PAIR= ['Z2', '-1']
LHS= Z2
RHS= -1
LINE= F= 12.5*L(0,x)L(1,y) + 30*L(0,x)L(2,y)L(2,z)
PAIR=['F', '12.5*L(0,x)L(1,y) + 30*L(0,x)L(2,y)L(2,z)']
LHS= F
RHS= 12.5*L(0,x)L(1,y) + 30*L(0,x)L(2,y)L(2,z)
My question is what is the next best step to process this output and use it in a mathematical script, especially assigning the L to mean a laguerre polynomial? I tried putting the LHS and RHS into a dictionary, but found it troublesome to put F in it due to the laguerre polynomials.
Any ideas are welcome. Perhaps I am overcomplicating this and there is a much simpler way to parse this file.
Many thanks in advance
Your parsing algorithm doesn't seem to work correctly, as the RHS of your variables dont produce the expected result.
Also the first re.sub block where you want to format the file seems overly complicated. Assuming every statement in your input file is terminated by a colon, you could get rid of all whitespace and newlines and seperate the statements using the following code:
linestring = open('file.txt','r').read()
strippedstring = linestring.replace('\n','').replace(' ','')
statements = re.split(':(?!=)',strippedstring)[:-1]
Then you iterate over the statements and split each one in LHS and RHS:
for st in statements:
lhs,rhs = re.split(':=',st)
print 'lhs=',lhs
print 'rhs=',rhs
In the next step, try to distinguish normal float variables and polynomials:
#evaluate rhs
try:
#interpret as numeric constant
f = float(rhs)
print " ",f
except ValueError:
#interpret as laguerre-polynomial
summands = re.split('\+', re.sub('-','+-',rhs))
for s in summands:
m = re.match("^(?P<factor>-?[0-9]*(\.[0-9]*)?)(?P<poly>(\*?L\([0-9]+,[a-z]\))*)", s)
if not m:
print ' polynomial misformatted'
continue
f = m.group('factor')
print ' factor: ',f
p = m.group('poly')
for l in re.finditer("L\((?P<a>[0-9]+),(?P<b>[a-z])\)",p):
print ' poly: L(%s,%s)' % (l.group("a"),l.group("b"))
This should work for your given example file.
I'm trying to print the top 10 lines of a file. single spaced. without spaces at the end
What can i change so that the output is single spaced instead of double spaced?
thanks
so far I've got:
with open('1.txt') as f:
i = 1
while i <= 10:
line = f.readline()
print line
i = i+1
f.close()
and i get an out put of:
1
2
3
4
5
6
7
8
9
10
easiest fix is to put a comma on the end of the print statement:
change
print line
to
print line,
You can simply use str.rstrip('\n') to do this. rstrip trims a trailing character, which in this case would be \n (newline character).
with open('1.txt') as f:
i = 1
while i <= 10:
line = f.readline().rstrip('\n')
print line
i = i+1
f.close()