如何从.txt文件

时间:2016-04-13 21:49:16

标签: python csv

 if "A" in columns and int(columns[5]) < int(columns[3]):
            print(columns)
            print (columns[3]) - (columns[5])

我在这里做错了什么?不用担心,我最近开始编码。

这是完整的代码:

import csv

FILE_NAME = "paintingJobs.txt" #I use this so that the file can be used easier
COL_HEADERS = ['Number', 'Date', 'ID', 'Total', 'Status', 'Paid']
NUM_COLS = len(COL_HEADERS)#This will insure that the header of each column fits into the length of the data

# read file once to determine maximum width of data in columns
with open(FILE_NAME) as f:
    reader = csv.reader(f, delimiter=',')
    # determine the maximum width of the data in each column
    max_col_widths = [len(col_header) for col_header in COL_HEADERS]
    for columns in reader:
        for i, col in enumerate(columns):
            if "A" in columns and int(columns[5]) < int(columns[3]):
                max_col_widths[i] = max(max_col_widths[i], len(repr(col)))
    # add 1 to each for commas
    max_col_widths = [col_width+1 for col_width in max_col_widths]

# read file second time to display its contents with the headers
with open(FILE_NAME) as f:
    reader = csv.reader(f, delimiter=',')
    # display justified column headers
    print(' ' + ' '.join(col_header.ljust(max_col_widths[i])
                            for i, col_header in enumerate(COL_HEADERS)))
    # display justified column data
    for columns in reader:
        if "A" in columns and int(columns[5]) < int(columns[3]):
            print(columns)
            print (columns[3]) - (columns[5])`

这是我得到的错误:

line 72, in Option_B
print (columns[3]) - (columns[5])
  

TypeError:不支持的操作数类型 - :'NoneType'和'str'

1 个答案:

答案 0 :(得分:0)

您应该在打印之前转换您的数字:

print("The result: ")
print(int(column[3]) - int(column[5]))