简体   繁体   English

Python不一致使用制表符写入csv文件

[英]Python inconsistent use of tabs writing to csv file

I'm getting an error writing a 'title' line to a csv file: 我在将“ title”行写入csv文件时遇到错误:

 File ".\aws_ec2_list_instances.py", line 58
    title_writer.writerow("AWS Master Instance List " + today)
                                                             ^
TabError: inconsistent use of tabs and spaces in indentation

I have a variable called today that I want to use: 我有一个今天要使用的变量:

today = datetime.today()
today = today.strftime("%m-%d-%Y")

This is the line causing the error: 这是导致错误的行:

title_writer = csv.writer(output_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
title_writer.writerow("AWS Master Instance List " + today)

I want the date as represented by the today variable listed next to the title. 我想要日期由标题旁边列出的today变量表示。

How can I do this correctly? 如何正确执行此操作?

You can fix this with a Find and Replace operation on your code: 您可以使用代码上的“查找并替换”操作来解决此问题:

Find: tab '\t'

Replace with: four spaces '    '

Having both tabs and spaces will make python unhappy, pick one and stick with it, I suggest spaces. 同时使用制表符和空格会使python不满意,请选择一个并坚持使用它,我建议使用空格。

In fact, depending on what you are using to write your code, you can have this done automatically if you press tab . 实际上,根据您正在使用的代码编写方式,如果您按tab可以自动完成此操作。 In Notepad++ it is under Settings > Preferences > Language > Replace by space 在Notepad ++中,它位于“设置”>“首选项”>“语言”>“按空格替换”下

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM