Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

python script for .dat file

1,659 views
Skip to first unread message

Muhammad Ali

unread,
Apr 5, 2016, 11:23:29 AM4/5/16
to

Hello,

Could any body tell me a general python script to generate .dat file after the extraction of data from more than 2 files, say file A and file B?

Or could any body tell me the python commands to generate .dat file after the extraction of data from two or more than two files?

I have to modify some python code.

Looking for your valuable posts.

Thank you.

Ali

Joel Goldstick

unread,
Apr 5, 2016, 11:30:27 AM4/5/16
to
> --
> https://mail.python.org/mailman/listinfo/python-list

What exactly is a .dat file? and how is it different from any other
file? Is it binary or text data?


--
Joel Goldstick
http://joelgoldstick.com/blog
http://cc-baseballstats.info/stats/birthdays

Muhammad Ali

unread,
Apr 5, 2016, 11:44:21 AM4/5/16
to
It is text data.

Mark Lawrence

unread,
Apr 5, 2016, 12:02:35 PM4/5/16
to
https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files

--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

Oscar Benjamin

unread,
Apr 5, 2016, 12:07:54 PM4/5/16
to
On 5 April 2016 at 16:44, Muhammad Ali <muhammad...@gmail.com> wrote:
> On Tuesday, April 5, 2016 at 8:30:27 AM UTC-7, Joel Goldstick wrote:
>> On Tue, Apr 5, 2016 at 11:23 AM, Muhammad Ali
>> <muhammad...@gmail.com> wrote:
>> >
>> > Could any body tell me a general python script to generate .dat file after the extraction of data from more than 2 files, say file A and file B?
>> >
>> > Or could any body tell me the python commands to generate .dat file after the extraction of data from two or more than two files?
>> >
>> > I have to modify some python code.
>>
>> What exactly is a .dat file? and how is it different from any other
>> file? Is it binary or text data?
>
> It is text data.

You haven't provided enough information for someone to answer your
question. This is a text mailing list so if a .dat file is text then
you can paste here an example of what it would look like. What would
be in your input files and what would be in your output files? What
code have you already written?

If the file is large then don't paste its entire content here. Just
show an example of what the data would look like if it were a smaller
file (maybe just show the first few lines of the file).

Probably what you want to do is easily achieved with basic Python
commands so I would recommend to have a look at a tutorial. There are
some listed here:
https://wiki.python.org/moin/BeginnersGuide/NonProgrammers

Also the tutor mailing list is probably more appropriate for this
level of question:
https://mail.python.org/mailman/listinfo/tutor

--
Oscar

Muhammad Ali

unread,
Apr 5, 2016, 12:28:05 PM4/5/16
to
Input and outout files are text files. e.g:
#KptCoord #E-E_Fermi #delta_N
0.0000 -22.0000 0.000E+00
0.0707 -22.0000 0.000E+00
0.1415 -22.0000 0.000E+00
0.2122 -22.0000 0.000E+00
0.2830 -22.0000 0.000E+00
0.3537 -22.0000 0.000E+00
0.4245 -22.0000 0.000E+00
0.4952 -22.0000 0.000E+00
0.5660 -22.0000 0.000E+00
0.6367 -22.0000 0.000E+00
0.7075 -22.0000 0.000E+00
0.7782 -22.0000 0.000E+00
0.8490 -22.0000 0.000E+00
0.9197 -22.0000 0.000E+00
0.9905 -22.0000 0.000E+00

Michael Selik

unread,
Apr 5, 2016, 4:36:15 PM4/5/16
to
What code have you written so far?
> --
> https://mail.python.org/mailman/listinfo/python-list

Mark Lawrence

unread,
Apr 5, 2016, 4:48:16 PM4/5/16
to
On 05/04/2016 21:35, Michael Selik wrote:
> What code have you written so far?
>

Would you please not top post on this list, it drives me nuts!!!

Joel Goldstick

unread,
Apr 5, 2016, 5:24:02 PM4/5/16
to
On Tue, Apr 5, 2016 at 4:47 PM, Mark Lawrence via Python-list
<pytho...@python.org> wrote:
> On 05/04/2016 21:35, Michael Selik wrote:
>>
>> What code have you written so far?
>>
>
> Would you please not top post on this list, it drives me nuts!!!
>
>
A short drive? ;)

Muhammad Ali

unread,
Apr 5, 2016, 5:49:14 PM4/5/16
to
Would any one paste here some reference/sample python code for such extraction of data into text file?

Erik

unread,
Apr 5, 2016, 6:47:28 PM4/5/16
to
Hi Muhammad,

On 05/04/16 22:49, Muhammad Ali wrote:
> Would any one paste here some reference/sample python code for such extraction of data into text file?

You haven't really explained what you want to achieve. What is the input
format, what is the output format, what constraints are there on those
two formats and the data within them and what transformation between the
two are you trying to perform?

If you're on a Linux system, then:

$ cat A.dat B.dat C.dat > result.txt

... will do something that is close to what you have asked for so far.
But I bet it's not what you want.

E.

Ben Finney

unread,
Apr 5, 2016, 7:11:05 PM4/5/16
to
Muhammad Ali <muhammad...@gmail.com> writes:

> Would any one paste here some reference/sample python code for such
> extraction of data into text file?

We're not going to write it for you.

Please show what code you have written so far; a small, self-contained
code example.

Explain what you expect that code to do, and what happens instead, and
how that is different from what you expect.

--
\ “I distrust those people who know so well what God wants them |
`\ to do to their fellows, because it always coincides with their |
_o__) own desires.” —Susan Brownell Anthony, 1896 |
Ben Finney

Steven D'Aprano

unread,
Apr 5, 2016, 9:10:19 PM4/5/16
to
On Wed, 6 Apr 2016 07:49 am, Muhammad Ali wrote:

> Would any one paste here some reference/sample python code for such
> extraction of data into text file?

column1 = ['aa', 'bb', 'cc', 'dd', 'ee', 'ff'] # strings
column2 = [97, 105, 16, 4, 48, 274] # ints
column3 = [12.345, 7.821, -4.034, 19.034, 23.0, 42.0] # floats


# Open the file for writing. It will be closed
# automatically at the end of the block.
with open("myfile.dat", "w") as f:
for t in zip(column1, column2, column3):
line = "%-9s %-9d %-9f\n" % t
print(line, end='')
f.write(line)



The function zip(...) combines one element from each argument into a tuple
of three values, like

t = ('aa', 97, 12.345)
t = ('bb', 105, 7.821)

etc. The string formatting line:

line = "%-9s %-9d %-9f\n" % t


builds a new string. The code %s takes any object, including strings, and
inserts it into the new string. %d requires an int, and %f requires a
float. The \n at the end inserts a newline.

The -9 inside the formatting codes sets the width and justification:

%-9d

formats an integer in nine columns, justified on the left, while %9d
justifies to the right:


py> print("%-9d." % 23)
23 .
py> print("%9d." % 23)
23.


Finally, the line is printed so you can see what it looks like, and written
to the file.


--
Steven

0 new messages