473,883 Members | 1,619 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

help: output arrays into file as column

bei
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

Jul 27 '06 #1
4 2188
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

Jul 27 '06 #2
bei
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
Jul 27 '06 #3
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
Jul 27 '06 #4
bei
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
Jul 28 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2862
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 (separated by commas). The first column contains a time stamp, the second column contains a...
4
2121
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 having trouble
8
5488
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 debug it in PHP designer, it works with no problems, I get the test email. If
7
1562
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 the output file has 2 columns and 42 rows. #include <stdlib.h> #include <stdio.h>
8
1612
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).
0
2266
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 in hopes on not having to trouble others on those. But here it goes...
11
4023
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>
3
2645
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 greatly appreciated!!! This is what I have so far: #include <iostream> #include <fstream>
5
2516
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 open the template in the editor. */
0
9944
marktang
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9793
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10752
jinu1996
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10420
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9582
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5996
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4619
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
2
4225
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3238
bsmnconsultancy
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.