Hi,
I am trying to write several arrays into one file, with one arrays in
one column. Each array (column) is seperated by space.
ie. a=[1,2,3, 4] b=[5,6,7,8] c=[9,10,11,12]
1 5 9
2 6 10
3 7 11
4 8 12
Now I use the function file.writelines (a), file.writelines (b),
file.writelines (c). And the output is a sequence of strings without
newlines between a, b ,c . Also each array stays in row other than
column.
I am a new comer to python.Any idea about this is appreciated!
Bei 4 2169
bei wrote:
Hi,
I am trying to write several arrays into one file, with one arrays in
one column. Each array (column) is seperated by space.
ie. a=[1,2,3, 4] b=[5,6,7,8] c=[9,10,11,12]
1 5 9
2 6 10
3 7 11
4 8 12
Now I use the function file.writelines (a), file.writelines (b),
file.writelines (c). And the output is a sequence of strings without
newlines between a, b ,c . Also each array stays in row other than
column.
I am a new comer to python.Any idea about this is appreciated!
Bei
Hi Bei,
file.writelines () works with lists of strings, not lists of numbers, so
I'm going to assume that a, b, and c are already lists of strings..
You can use the zip() function to change the lists in the way you want
to:
|>a=['1','2','3','4']; b=['5','6','7','8']; c=['9','10','11',' 12']
|>zip(a, b, c)
[('1', '5', '9'), ('2', '6', '10'), ('3', '7', '11'), ('4', '8', '12')]
now that you have the data for each line, you can combine them with the
string join() method:
|>data = zip(a, b, c)
|>for datum in data:
.... print ' '.join(datum)
....
1 5 9
2 6 10
3 7 11
4 8 12
You can print to an open file object, so the above loop will do what
you need:
|>f = open('output.tx t', 'w')
|>for datum in data:
.... print >f, ' '.join(datum)
....
|>f.close()
|>print open('output.tx t').read()
1 5 9
2 6 10
3 7 11
4 8 12
I hope that helps!
Peace,
~Simon
Hi,Simon,
Thanks for your reply.It's very helpful :)
But I am sorry for my given example.Actuall y, my data in the arrays are
all float point datas.And I use integer in the example.The code is like
this.
("x,v,...,h" are floating point number arrays)
pos=str(x)
vel=str(v)
ene=str(u)
den=str(rho)
pre=str(P)
hms=str(h)
datas=zip(pos,v el,ene,den,pre, hms)
filename="data. dat"
file=open(filen ame,"w")
for datum in datas:
print >>file, ' '.join(datum)
file.close()
However, the result seperate each point in floating number , but not
regard them as a whole. It's like this :
[ [ [ [ [ [
- 0 1 1 1 0
0 . . . . .
.. 0 4 0 0 0
5 , 9 , , 0
9 9 3
9 0 9 1 1 7
9 . 9 . . 6
9 0 9 0 0 8
9 , 9 , , 7
9 9 5
9 0 9 1 1 ,
9 . 9 . .
9 0 9 0 0 0
9 , 9 , , .
9 9 0
9 0 9 1 1 0
9 . 9 . . 3
9 0 8 0 0 7
9 , , , , 6
8 8
, 0 1 1 1 7
. . . . 5
- 0 4 0 0 ,
0 , 9 , ,
.. 9 0
5 0 9 1 1 .
9 . 9 . . 0
6 0 9 0 0 0
9 , 9 , , 3
9 9 7
2 0 9 1 1 6
4 . 9 . . 8
8 0 9 0 0 7
1 , 9 , , 5
2 9 ,
0 0 9 1 1
3 . 9 . . 0
0 0 8 0 0 .
0 , , , , 0
7 0
5 0 1 1 1 3
2 . . . . 7
, 0 4 0 0 6
, 9 , , 8
- 9 7
0 0 9 1 1 5
.. . 9 . . ,
5 0 9 0 0
9 , 9 , , 0
3 9 .
9 0 9 1 1 0
8 . 9 . . 0
4 0 9 0 0 3
9 , 9 , , 7
6 9 6
2 0 9 1 1 8
4 . 9 . . 7
0 0 8 0 0 5
6 , , , , ,
0
1 0 1 1 1 0
5 . . . . .
0 0 4 0 0 0
6 , 9 , , 0
, 9 3
0 9 1 1 7
- . 9 . . 6
0 0 9 0 0 8
.. , 9 , , 7
5 9 5
9 0 9 1 1 ,
0 . 9 . .
9 0 9 0 0 0
7 , 9 , , .
7 9 0
4 0 9 1 1 0
4 . 9 . . 3
3 0 8 0 0 7
6 , , , , 6
0 8
9 0 1 1 1 7
0 . . . . 5
2 0 4 0 0 ,
2 , 9 , ,
4 9 0
9 0 9 1 1 .
, . 9 . . 0
0 9 0 0 0
......
Do you have a way to work this out? Thank you very much, And sorry
again for my incorrect example.
Bei
Simon Forman wrote:
bei wrote:
Hi,
I am trying to write several arrays into one file, with one arrays in
one column. Each array (column) is seperated by space.
ie. a=[1,2,3, 4] b=[5,6,7,8] c=[9,10,11,12]
1 5 9
2 6 10
3 7 11
4 8 12
Now I use the function file.writelines (a), file.writelines (b),
file.writelines (c). And the output is a sequence of strings without
newlines between a, b ,c . Also each array stays in row other than
column.
I am a new comer to python.Any idea about this is appreciated!
Bei
Hi Bei,
file.writelines () works with lists of strings, not lists of numbers, so
I'm going to assume that a, b, and c are already lists of strings..
You can use the zip() function to change the lists in the way you want
to:
|>a=['1','2','3','4']; b=['5','6','7','8']; c=['9','10','11',' 12']
|>zip(a, b, c)
[('1', '5', '9'), ('2', '6', '10'), ('3', '7', '11'), ('4', '8', '12')]
now that you have the data for each line, you can combine them with the
string join() method:
|>data = zip(a, b, c)
|>for datum in data:
... print ' '.join(datum)
...
1 5 9
2 6 10
3 7 11
4 8 12
You can print to an open file object, so the above loop will do what
you need:
|>f = open('output.tx t', 'w')
|>for datum in data:
... print >f, ' '.join(datum)
...
|>f.close()
|>print open('output.tx t').read()
1 5 9
2 6 10
3 7 11
4 8 12
I hope that helps!
Peace,
~Simon
bei a écrit :
<otPlease don't top-post</ot>
Hi,Simon,
Thanks for your reply.It's very helpful :)
But I am sorry for my given example.Actuall y, my data in the arrays are
all float point datas.And I use integer in the example.The code is like
this.
("x,v,...,h" are floating point number arrays)
Arrays or lists ?
pos=str(x)
Why on earth are you doing this ?
vel=str(v)
ene=str(u)
den=str(rho)
pre=str(P)
hms=str(h)
datas=zip(pos,v el,ene,den,pre, hms)
datas = zip(v, u, rho, P, h)
filename="data. dat"
file=open(filen ame,"w")
This shadows the builtin 'file' type. Using another name may be a good idea.
for datum in datas:
print >>file, ' '.join(datum)
print >file, ' '.join(map(str, datum))
file.close()
>
However, the result seperate each point in floating number , but not
regard them as a whole. It's like this :
[ [ [ [ [ [
- 0 1 1 1 0
0 . . . . .
. 0 4 0 0 0
5 , 9 , , 0
9 9 3
(snip)
One of the nice things with Python is the interactive shell. Let's use it:
Python 2.4.1 (#1, Jul 23 2005, 00:37:37)
[GCC 3.3.4 20040623 (Gentoo Linux 3.3.4-r1, ssp-3.3.2-2, pie-8.7.6)] on
linux2
Type "help", "copyright" , "credits" or "license" for more information.
>>a = map(float, range(0,3)) a
[0.0, 1.0, 2.0]
>>b = map(float, range(12, 15)) c = map(float, range(7, 10)) a, b, c
([0.0, 1.0, 2.0], [12.0, 13.0, 14.0], [7.0, 8.0, 9.0])
>>str(a)
'[0.0, 1.0, 2.0]'
>>list(str(a) )
['[', '0', '.', '0', ',', ' ', '1', '.', '0', ',', ' ', '2', '.', '0', ']']
>>zip(str(a), str(b), str(c))
[('[', '[', '['), ('0', '1', '7'), ('.', '2', '.'), ('0', '.', '0'),
(',', '0', ','), (' ', ',', ' '), ('1', ' ', '8'), ('.', '1', '.'),
('0', '3', '0'), (',', '.', ','), (' ', '0', ' '), ('2', ',', '9'),
('.', ' ', '.'), ('0', '1', '0'), (']', '4', ']')]
>>zip(a, b, c)
[(0.0, 12.0, 7.0), (1.0, 13.0, 8.0), (2.0, 14.0, 9.0)]
>>>
HTH
Perfect! It works. Thanks Bruno.
Bruno Desthuilliers wrote:
bei a écrit :
<otPlease don't top-post</ot>
Hi,Simon,
Thanks for your reply.It's very helpful :)
But I am sorry for my given example.Actuall y, my data in the arrays are
all float point datas.And I use integer in the example.The code is like
this.
("x,v,...,h" are floating point number arrays)
Arrays or lists ?
pos=str(x)
Why on earth are you doing this ?
vel=str(v)
ene=str(u)
den=str(rho)
pre=str(P)
hms=str(h)
datas=zip(pos,v el,ene,den,pre, hms)
datas = zip(v, u, rho, P, h)
filename="data. dat"
file=open(filen ame,"w")
This shadows the builtin 'file' type. Using another name may be a good idea.
for datum in datas:
print >>file, ' '.join(datum)
print >file, ' '.join(map(str, datum))
file.close()
However, the result seperate each point in floating number , but not
regard them as a whole. It's like this :
[ [ [ [ [ [
- 0 1 1 1 0
0 . . . . .
. 0 4 0 0 0
5 , 9 , , 0
9 9 3
(snip)
One of the nice things with Python is the interactive shell. Let's use it:
Python 2.4.1 (#1, Jul 23 2005, 00:37:37)
[GCC 3.3.4 20040623 (Gentoo Linux 3.3.4-r1, ssp-3.3.2-2, pie-8.7.6)] on
linux2
Type "help", "copyright" , "credits" or "license" for more information.
>>a = map(float, range(0,3))
>>a
[0.0, 1.0, 2.0]
>>b = map(float, range(12, 15))
>>c = map(float, range(7, 10))
>>a, b, c
([0.0, 1.0, 2.0], [12.0, 13.0, 14.0], [7.0, 8.0, 9.0])
>>str(a)
'[0.0, 1.0, 2.0]'
>>list(str(a) )
['[', '0', '.', '0', ',', ' ', '1', '.', '0', ',', ' ', '2', '.', '0', ']']
>>zip(str(a), str(b), str(c))
[('[', '[', '['), ('0', '1', '7'), ('.', '2', '.'), ('0', '.', '0'),
(',', '0', ','), (' ', ',', ' '), ('1', ' ', '8'), ('.', '1', '.'),
('0', '3', '0'), (',', '.', ','), (' ', '0', ' '), ('2', ',', '9'),
('.', ' ', '.'), ('0', '1', '0'), (']', '4', ']')]
>>zip(a, b, c)
[(0.0, 12.0, 7.0), (1.0, 13.0, 8.0), (2.0, 14.0, 9.0)]
>>>
HTH
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: scadav |
last post by:
I am new to Perl and I am trying for figure out how to solve this
problem. If anyone can give me some suggestions, I would greatly
appreciate it.
I am trying to read a log file and generate some statistics from it.
For simplicity purposes (only) I have edited some of my logs and code.
Below is an example of a log file which has 3 columns...
|
by: Josh |
last post by:
Howdy
i am newb somewhat to programing and i was just for fun trying to
compile a program that asks the user for an odd int less than 22 and
then returns this
*****************
******* *********
****** ********
***** *******
etc. the first line reps. the number enterd by user
but i am having trouble i can get the first line but am...
|
by: baustin75 |
last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie
only when debugging in php designer 2005
--------------------------------------------------------------------------------
Hello,
I have a very simple problem but cannot seem to figure it out. I have a
very simple php script that sends a test email to myself. When I...
|
by: pillip |
last post by:
I am trying to use fopen and fget to input two files and then output them
into one file. Each input file has two columns and 20 rows, however since
the first column in each input file is same ( numbers 0...19), i want the
output file to have 3 columns and 20 rows. Right now i am using a one
dimensional array "array1" to input each file:, and...
|
by: hothead098 |
last post by:
ASSIGNMENT (4) USING AND MANIPUPATING ARRAYS (Chapter 10 material)
For this assignment you are to:
1) Create and manage arrays
a) One of type integers (containing 10 elements).
b) One of type strings (containing 20 elements).
c) One of type char (containing 30 elements).
| |
by: ward |
last post by:
Greetings.
Ok, I admit it, I bit off a bit more than I can chew.
I need to complete this "Generate Report" page for my employer and I'm
a little over my head. I could use some additional assistance. I say
additional because I've already had help which is greatly appreciated.
I do try to take the time and understand the provided script...
|
by: c19h28o2 |
last post by:
Hi,
Guy's I know there are several posts about this, however I do not want
to read them as answers are undoubtedly posted!
Here is my attempt but I'm slightly stuck. I'm not looking for the
answer I'm looking for a point in the right direction.....
#include <stdio.h>
|
by: Beginner01 |
last post by:
I need some help reading in a file that has a column with the student's name and then a column with their score.
I need to input this into 2 seperate arrays. One for the names and one for the scores.
I have to use a String Array and a Int Array. I can't figure out how to get it into a String array. I keep getting errors.
Any help would be...
|
by: Stephen3776 |
last post by:
I am doing an inventory control progam and trying to output a multiple array, I am getting an illegal conversion error java.lang.double !d. Can somebody tell me what I am doing wrong or if there is another way?
/*
* Main.java
*
* Created on April 29, 2007, 6:57 PM
*
* To change this template, choose Tools | Template Manager
* and...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |