【问题标题】:Reading from the screen and writing it to a file in python?从屏幕读取并将其写入python中的文件?
【发布时间】:2016-04-11 18:13:56
【问题描述】:

我正在屏幕上打印一些字符串,稍后我想将它读入文件。我无法将字符串读取到文件中。我在这里犯了一个错误- filehandle.write(.read()) 。有人可以告诉我吗?

【问题讨论】:

  • 错误信息是什么?我无法想象“.read()”是 python 合成器
  • filehandle.write(.read()) 不是有效的 Python。为什么你认为它应该起作用?从小处着手,例如,尝试将单词"hello" 写入屏幕和文件。在你设法做到之前不要再往前走。
  • 文件 ".\Parsing_throug_folder_DEU.py",第 164 行,在 outfilename = filename(".txt") TypeError: 'str' object is not callable
  • 请发布您的代码。看起来你缺乏基本的 Python 知识。文件名是什么方法? Python 认为它是一个字符串。
  • @suni 不客气! ... >:(

标签: python json file python-2.7 python-3.x


【解决方案1】:

我不知道你需要 JSON 做什么,但这是如何打印字符串并将其写入 python 中的文件:

string = "text like hello world"
print(string)
f = open('fileaddress', 'w')
f.write(string)

第 1 行:将字符串设置为“text like hello world”

第 2 行:在屏幕上打印字符串

第 3 行:打开文件进行写入。也可以用 'r' 而不是 'w' 来打开它的阅读模式。阅读https://docs.python.org/2/tutorial/inputoutput.html 寻求帮助。

第 4 行:将文件的内容设置为我们声明的字符串。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多