Python 如何从.txt文件中删除2个值
TypeError:-:“NoneType”和“str”的操作数类型不受支持Python 如何从.txt文件中删除2个值,python,csv,Python,Csv,TypeError:-:“NoneType”和“str”的操作数类型不受支持 在打印之前,您可能需要转换数字: line 72, in Option_B print (columns[3]) - (columns[5]) 发布您的示例输入、输出、您尝试过的内容以及您期望的结果。您是否收到错误消息,如果是,是什么?你得到的结果不正确吗?什么是专栏?你期望得到什么结果?您正在使用Python 2吗?好的,我已经更新了postCan,您可以发布完整的错误消息吗?也就是说,错误发生在哪一行。错误消息清
在打印之前,您可能需要转换数字:
line 72, in Option_B
print (columns[3]) - (columns[5])
发布您的示例输入、输出、您尝试过的内容以及您期望的结果。您是否收到错误消息,如果是,是什么?你得到的结果不正确吗?什么是专栏?你期望得到什么结果?您正在使用Python 2吗?好的,我已经更新了postCan,您可以发布完整的错误消息吗?也就是说,错误发生在哪一行。错误消息清楚地表明,
columns[3]
是None
,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])
print("The result: ")
print(int(column[3]) - int(column[5]))