简体   繁体   English

如何保存该文件txt?

[英]How can I save this file txt?

I am trying to save a .txt file, previously cleaning from the previous .txt, but it is not saved and gives me an error in the final print [0]. 我正在尝试保存一个.txt文件,该文件先前是从以前的.txt文件中清除的,但是没有保存,最终打印时出现错误[0]。 I have been watching video and they do it. 我一直在观看视频,他们正在这样做。

I am using jupyter notebook. 我正在使用jupyter笔记本。 I'm sorry for my English it is low. 对不起,我的英语太差了。

archivo = open ("salida_tweets.txt")
linea = archivo.readline()
tweet=linea.split(',"text":"')

s = len(tweet)

for i in range(1,s):
    final = tweet[i].split('","truncated"')
    print final [0]

 File "<ipython-input-6-acef6c26b974>", line 9
    print final [0]
              ^
SyntaxError: Missing parentheses in call to 'print'. Did you mean 
print(final [0])?

Your error is syntax error its because in the video you were watching they were using python2.7 but your current version is 3 so use 您的错误是语法错误,因为您在观看视频的过程中使用的是python2.7,但当前版本为3,请使用

print(final[0])

In python3 print has been used as a function so this throws an error 在python3中,print已用作函数,因此会引发错误

Python has different access specifiers that specify the modes the file is open in Python具有不同的访问说明符,用于指定文件打开的模式

file=open("file_address.txt","w")

This will open file in write mode to write an string in a file you can simply use 这将以写入模式打开文件,以在您可以简单使用的文件中写入字符串

file.write("anything")

"anything" will be written in the file If you want to append the new information just open file in append mode 文件中将写入“任何内容”。如果要附加新信息,只需以附加模式打开文件

file=open("file_address.txt","a")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将所有值保存在单个txt文件中? - how can I save all the values in a single txt file? 如何将 PyMOL 中的命令输出保存到 txt 文件? - How can I save command outputs in PyMOL to a txt file? 如何将已解析的xml保存为txt? - How can I save parsed xml as txt? 如何将列表转换为字符串,以便将其保存在 txt 文件中? - How do I turn a list into a string so I can save it in a txt file? 如何在txt文件中保存矩阵,然后在python中再次将其作为矩阵打开? - How can I save a matrix in a txt file and then open it again as a matrix in python? 如何更轻松地将txt文件中的值保存到变量? python line.split - How can I more easily save value in txt file to variable? python line.split 如何将我的 python 输出从 XML 文件保存到 txt - how can I save my python output from XML file to txt 如何将终端 output 从某个文件夹(ls 命令)保存到 txt 文件? 对于 Pycharm - How can I save terminale output from certain folder (ls command) to txt file? For Pycharm Discord.py 如何获取用户的语音通道 ID 并将其保存到 .txt 文件? - Discord.py how can I get the Voice channel ID of a user AND save it to a .txt file? 如何将iPython Notebook的全部输出另存为.txt文件? - How can I save my entire output from iPython notebook as .txt file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM