需要将文件内容写入.txt [英] Need to write contents of file to a .txt

查看:48
本文介绍了需要将文件内容写入.txt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我对python很新,实际上只玩了一个星期。这是我第一次尝试编程,我正在尝试生成一些随机浮点数,然后将这个随机生成的结果填充到.txt文件中。到目前为止,我只能生成随机整数并创建一个空白.txt。有人可以帮忙吗?如何让我的注意力转向浮点数我尝试过使用浮点数我的数字前面的数据类型,但这不起作用。此外,如何生成随机数后,我会让它们填充创建的.txt文件。


非常感谢


这是我到目前为止的代码:


#随机数生成器

#此脚本生成随机数

#然后脚本将结果写入文本文件


text_file = open(" points.txt"," w")

#text_file.write(i)


导入随机


表示范围内的数字(1,201):

print"%5d" %(random.randrange(1,201)),


如果数字%6 == 0:

打印



#text_file.writelines(数字)


text_file.close()


raw_input(" \ n \ n按回车键退出。)

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.")

推荐答案


你好,


我对python很新,实际上只玩了一个星期。这是我第一次尝试编程,我正在尝试生成一些随机浮点数,然后将这个随机生成的结果填充到.txt文件中。到目前为止,我只能生成随机整数并创建一个空白.txt。有人可以帮忙吗?如何让我的注意力转向浮点数我尝试过使用浮点数我的数字前面的数据类型,但这不起作用。此外,如何生成随机数后,我会让它们填充创建的.txt文件。


非常感谢


这是我到目前为止的代码:


#随机数生成器

#此脚本生成随机数

#然后脚本将结果写入文本文件


text_file = open(" points.txt"," w")

#text_file.write(i)


导入随机


表示范围内的数字(1,201):

print"%5d" %(random.randrange(1,201)),


如果数字%6 == 0:

打印



#text_file.writelines(数字)


text_file.close()


raw_input(" \ n \ n按回车键退出。)
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:

展开 | 选择 | Wrap | 行号


这是另一种方式:
展开 | 选择 | Wrap | 行号


谢谢bvdet,我会看一下,希望我可以修改它,看起来很有趣你教会了我一些新的todya伟大! :D。


此致,


Deathiwng
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


这篇关于需要将文件内容写入.txt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆