473,387 Members | 1,592 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Need to write contents of file to a .txt

Deathwing
Hello,

I'm fairly new to python and have only been playing around with it for one week actually. It's my first attempt at programming and I'm trying to generate some random floats and then have the results of this random generation be filled into a .txt file. So far I have only been able to generate random intergers and create a blank .txt. Can anyone help ? How do I get my ints to turn to floats I have tried using the "float" datatype infront of my numbers but that does not work. Also, how after having generated the random numbers do I get them to populate the .txt file that is created.

Thanks so very much

Here is my code thus far:

# Random number generator
# This script generates random numbers
# The script then writes the result to a text file


text_file = open("points.txt", "w")
#text_file.write(i)

import random

for numbers in range( 1, 201 ):
print "%5d" % ( random.randrange( 1, 201 ) ),

if numbers % 6 == 0:
print


#text_file.writelines(numbers)

text_file.close()

raw_input("\n\nPress the enter key to exit.")
Mar 28 '07 #1
13 2201
bvdet
2,851 Expert Mod 2GB
Hello,

I'm fairly new to python and have only been playing around with it for one week actually. It's my first attempt at programming and I'm trying to generate some random floats and then have the results of this random generation be filled into a .txt file. So far I have only been able to generate random intergers and create a blank .txt. Can anyone help ? How do I get my ints to turn to floats I have tried using the "float" datatype infront of my numbers but that does not work. Also, how after having generated the random numbers do I get them to populate the .txt file that is created.

Thanks so very much

Here is my code thus far:

# Random number generator
# This script generates random numbers
# The script then writes the result to a text file


text_file = open("points.txt", "w")
#text_file.write(i)

import random

for numbers in range( 1, 201 ):
print "%5d" % ( random.randrange( 1, 201 ) ),

if numbers % 6 == 0:
print


#text_file.writelines(numbers)

text_file.close()

raw_input("\n\nPress the enter key to exit.")
Maybe you can modify this to meet your requirements:
Expand|Select|Wrap|Line Numbers
  1. import random
  2.  
  3. # generate 20 random numbers and write to a text file
  4.  
  5. f = open(r'path\dir\file_name', 'w')
  6. for i in range(20):
  7.     f.write('%.8f\n' % (random.random()))
  8.  
  9. f.close()
  10.  
  11. '''
  12. 0.49790300
  13. 0.22560614
  14. 0.94740230
  15. 0.76629025
  16. 0.54590250
  17. 0.01583565
  18. 0.87463924
  19. 0.39809160
  20. 0.06935606
  21. 0.69546613
  22. 0.79916955
  23. 0.53687077
  24. 0.56587108
  25. 0.78859956
  26. 0.91774516
  27. 0.63878437
  28. 0.38448525
  29. 0.60584063
  30. 0.30951708
  31. 0.56703802
  32. '''
Please use code tags in your posts. See reply guidelines. HTH :)
Mar 28 '07 #2
bvdet
2,851 Expert Mod 2GB
Here is another way:
Expand|Select|Wrap|Line Numbers
  1. import random
  2.  
  3. f = open(r'path\dir\file_name', 'w')
  4. f.writelines(['%.8f\n' % (random.random()) for _ in range(20)])
  5. f.close()
Mar 28 '07 #3
Thanks bvdet, I'll take a look at it I hope I can modify it, it looks very interesting you have taught me something new todya great! :"D.

Sincerely,

Deathiwng
Mar 28 '07 #4
ghostdog74
511 Expert 256MB
eg
Expand|Select|Wrap|Line Numbers
  1. python -c "import random; print '\n'.join([str(random.randint(1,201)) for i in range(20)])" > file      
  2.  
Mar 28 '07 #5
eg
Expand|Select|Wrap|Line Numbers
  1. python -c "import random; print '\n'.join([str(random.randint(1,201)) for i in range(20)])" > file      
  2.  
could you explain? I'm sorry I don't understand
Mar 28 '07 #6
Here is another way:
Expand|Select|Wrap|Line Numbers
  1. import random
  2.  
  3. f = open(r'path\dir\file_name', 'w')
  4. f.writelines(['%.8f\n' % (random.random()) for _ in range(20)])
  5. f.close()
Hey bvdet thanks for the tip I have managed to adjust it somewhat but I need to figure out how to get the output/data in to x and y columns here is my modified code. Hope you can help.
Expand|Select|Wrap|Line Numbers
  1. # Random number generator
  2. # This script generates random numbers
  3. # The script then writes the result to a text file
  4. # Guillermo Paniagua - 03/27/07
  5. import random
  6.  
  7.  
  8.  
  9. f = open(r"grapes.txt", "w")
  10. #text_file.write(i)
  11.  
  12.  
  13.  
  14. for i in range( 1, 601 ):
  15.  
  16.  
  17.    f.write( "%5f\n" % ( random.randrange( 1, 601 ) )),
  18.  
  19.   #if numbers % 6 == 0:  
  20.      #print
  21.  
  22.  
  23. #text_file.writelines(numbers)
  24.  
  25. f.close()
  26.  
  27. raw_input("\n\nPress the enter key to exit.")
Mar 28 '07 #7
bvdet
2,851 Expert Mod 2GB
Hey bvdet thanks for the tip I have managed to adjust it somewhat but I need to figure out how to get the output/data in to x and y columns here is my modified code. Hope you can help.
Expand|Select|Wrap|Line Numbers
  1. # Random number generator
  2. # This script generates random numbers
  3. # The script then writes the result to a text file
  4. # Guillermo Paniagua - 03/27/07
  5. import random
  6.  
  7.  
  8.  
  9. f = open(r"grapes.txt", "w")
  10. #text_file.write(i)
  11.  
  12.  
  13.  
  14. for i in range( 1, 601 ):
  15.  
  16.  
  17.    f.write( "%5f\n" % ( random.randrange( 1, 601 ) )),
  18.  
  19.   #if numbers % 6 == 0:  
  20.      #print
  21.  
  22.  
  23. #text_file.writelines(numbers)
  24.  
  25. f.close()
  26.  
  27. raw_input("\n\nPress the enter key to exit.")
What do you mean by "get the output/data in to x and y columns"?. Instead of each number on a separate line, do you want the numbers in pairs?
Mar 28 '07 #8
What do you mean by "get the output/data in to x and y columns"?. Instead of each number on a separate line, do you want the numbers in pairs?
hi there and yep an x and y column you are correct I do want numbers in pairs. :)
Mar 28 '07 #9
bartonc
6,596 Expert 4TB
Please read "REPLY GUIDELINES" so that you know how to use CODE tags.
Then start using them. It's a real pain for others to try and see the structure of your code without them.

Thank you very much.
Mar 28 '07 #10
hi there and yep an x and y column you are correct I do want numbers in pairs. :)
it would also be good if the random floating point numbers where more than just always 12.00 , instead it would be nice to see 12.31,12.00, 14.56 etc... Do I have to change the datatype from float to decimal or something ? or use some type of math module ?
Mar 28 '07 #11
bvdet
2,851 Expert Mod 2GB
it would also be good if the random floating point numbers where more than just always 12.00 , instead it would be nice to see 12.31,12.00, 14.56 etc... Do I have to change the datatype from float to decimal or something ? or use some type of math module ?
Something like this?
Expand|Select|Wrap|Line Numbers
  1. import random
  2.  
  3. f = open(r'path\dir\file_name', 'w')
  4. f.writelines(['%12.8f %14.8f\n' % (random.random()*random.randint(1,600), random.random()*random.randint(1,600)) for _ in range(20)])
  5. f.close()
  6. '''
  7. 178.09115903   133.36811062
  8.  60.54302702    93.40949828
  9. 245.20980645   254.00913526
  10. 350.37618749    49.87972752
  11. 276.50622882   248.12002096
  12. 234.91464718     3.01132671
  13.   0.10043225   379.17639623
  14.  21.99813057   213.64247640
  15.   5.09396543     2.42861560
  16. 101.41723993    32.51914585
  17.  29.31167588   430.25596881
  18.  48.70798269    90.25232557
  19.  33.68899482   153.09551692
  20. 107.74691175   108.88171789
  21.   2.27747461    13.77821158
  22. 125.42570521    53.47742587
  23.  90.04062953    38.57455190
  24. 171.75925277   234.66057796
  25.   2.05780089   234.13322485
  26. 303.65883903    20.71402190
  27. '''
Mar 28 '07 #12
Yes that is right something like this... thanks so very much I think this will help alot. I will take a look at it and try to adapt it to see if it works. By the way Python is my new hobby I thought it would be cool to learn to program and thought I'd either learn using Python or IDL. So thanks alot looking over your suggestions has really helped me out :)
Mar 29 '07 #13
bvdet
2,851 Expert Mod 2GB
Yes that is right something like this... thanks so very much I think this will help alot. I will take a look at it and try to adapt it to see if it works. By the way Python is my new hobby I thought it would be cool to learn to program and thought I'd either learn using Python or IDL. So thanks alot looking over your suggestions has really helped me out :)
You are welcome. We are glad to help and appreciate the positive feedback. :)
Mar 30 '07 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: JDJones | last post by:
I'm trying to write a script that will read from a text list of songs that I burned onto my CD (Albums011.txt), then write to the database text the new text made ready for inserting into a...
0
by: Joan Bos | last post by:
Hi, Is there somewhere on the Internet a description of what a .NET application config file should contain? For our application, I have to write passwords encrypted in a config file. The...
1
by: Aalok | last post by:
This is what i want to do. Read a text file as an input and based on that file, Create an outpu text file which saves the contents of the input file in a specifi format in the output file. I...
0
by: RSB | last post by:
Hi Every one, i am trying to create a UserControl and i am passing a Array of strings to it. Now based on the Array elements i am creating the LinkButtons Dynamically. I am also passing a Event to...
15
by: Gan Quan | last post by:
I'm writing a c++ program that has many (100+) threads read/write files simultaneously. It works well if not considering the efficiency. The file i/o seems to be the bottleneck. This is my code...
2
kamill
by: kamill | last post by:
i need to write content of one text file into another text file. My code is working ,if i choose both files from same directory where my program reside..BUT,its not working if i select files from...
24
by: Bill | last post by:
Hello, I'm trying to output buffer content to a file. I either get an access violation error, or crazy looking output in the file depending on which method I use to write the file. Can anyone...
4
by: Paul David Buchan | last post by:
Hello, I'm attempting to write a program to read in database files (.dbf). When I do it all as a single procedure in main, everything works. However, what I really want, is to pass the database...
11
by: blunt | last post by:
trying to write a program to write the configuration files for a load of wireless access points. i've never been a good programmer and haven't done any for nearly a decade so have obviously made some...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
Oralloy
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,...
0
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...

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.