简体   繁体   English

使用python 3.x“ with open”功能编写文本文件错误

[英]Writing a text file error using python 3.x “with open” function

I am writing a text file with some of the data I analyzed using python. 我正在编写一个文本文件,其中包含我使用python分析的一些数据。 I am running into some error messages. 我遇到了一些错误消息。 Below is my code. 下面是我的代码。

sixteen=0.1
fifteen=0.3
fourteen=-.4
fourteen_revised=1
thirteen=2

with open('TMV_AVD.txt','w') as f:
    f. write('16+',sixteen+'\n','15+', fifteen+'\n','14+',\
             fourteen+'\n','14-',fourteen_revised+'\n', '13-', thirteen)

What I want in my text file is the following 我想要的文本文件如下

16+,0.1
15+,0.3
14+,-.4
14-,1
13-,2

The error message I get is the following. 我收到的错误消息如下。

ufunc 'add' did not contain a loop with signature matching types dtype('<U32') dtype('<U32') dtype('<U32').

I thought I understood with open function. 我以为我了解with open功能。 Your suggestions will be greatly appreciated. 您的建议将不胜感激。 Any suggestions? 有什么建议么?

It has nothing to do with open() and its context manager, it has to do with the concatenation of your data and mismatched types. 它与open()及其上下文管理器无关,它与数据和类型不匹配的串联有关。 Your example, tho, should throw a different error - I imagine the error you posted stems from some Pandas structure. 您的示例tho应该引发另一个错误-我想象您发布的错误源于某些Pandas结构。

You can let str.format() attempt to concatenate your data as: 您可以让str.format()尝试将数据串联为:

with open('TMV_AVD.txt', 'w') as f:
    f.write('16+,{}\n15+,{}\n14+,{}\n14-,{}\n13-,{}'.format(sixteen, fifteen, fourteen,
                                                            fourteen_revised, thirteen))

Or, if using Python 3.6+ you can directly build an f string : 或者,如果使用Python 3.6+,则可以直接构建f字符串

f.write(f"16+,{sixteen}\n15+,{fifteen}\n14+,{fourteen}\n14-,{fourteen_revised}\n13-,{thirteen}")

Or you'll have to manually turn your data into a proper format before concatenating. 或者,您必须在连接之前手动将数据转换为正确的格式。

Your logic is overly complicated. 您的逻辑过于复杂。 I strongly recommend you use a list of tuples or OrderedDict to store your variables. 我强烈建议您使用元组列表或OrderedDict来存储变量。 Then use a simple for loop: 然后使用一个简单的for循环:

d = [('sixteen', ('16+', '0.1')),
     ('fifteen', ('15+', '0.3')),
     ('fourteen', ('14+', '-.4')),
     ('fourteen_revised', ('14-', '1')),
     ('thirteen', ('13-', '2'))]

with open('TMV_AVD.txt', 'w') as f:
    for _, vals in d:
        f.write(','.join(vals)+'\n')

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

相关问题 在Python 3.x中写入文件 - Writing to a file in Python 3.x Python 3.x - 将生成的字母作为列表写入文本文件 - Python 3.x - writing generated letters as list to a text file 使用python 3.x从文本文件中找到匹配的关键字 - Finding a matching keyword from a text file using python 3.x 在Python 3.x中将文本文件转换为列表 - Converting a text file to a list in Python 3.x Python 3.X写入txt文件不会创建新行 - Python 3.X Writing to txt File will not create new lines 将 .csv 文件从 URL 读入 Python 3.x - _csv.Error:迭代器应该返回字符串,而不是字节(您是否以文本模式打开文件?) - Read .csv file from URL into Python 3.x - _csv.Error: iterator should return strings, not bytes (did you open the file in text mode?) 通过将列表保存在文本文件中并在使用时将其导入来使用列表列表。 的Python 3.X - Using a list of lists by saving it on a text file and importing it when using it. Python 3.X 使用python 3.x从文本文件中追加后,在匹配一行后添加一个值 - Adding up a value after matching a line using append from a text file using python 3.x python 3.x-基于文本的冒险游戏,保存游戏功能 - python 3.x - text based adventure game, save game function 分析文本文件中的Python 3.x中的某些属性 - Analyze a text file for certain attributes in Python 3.x
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM