Python 当我运行以下代码时,我得到了以下错误:ValueError:invalid literal for int(),以10为基数:“0”;(1,0,&“x27;星期五&”x27;);

Python 当我运行以下代码时,我得到了以下错误:ValueError:invalid literal for int(),以10为基数:“0”;(1,0,&“x27;星期五&”x27;);,python,Python,当我运行下面的代码时,我得到 ValueError: invalid literal for int() with base 10: "(1, 0, 'Friday')" monthwise = list(map(int, read_from_csv(infile_csv, month=True)[0])) . 指向该行: monthwise = list(map(int, read_from_csv(infile_csv, month=True)[0])) . 我已经包含了示例.cs

当我运行下面的代码时,我得到

ValueError: invalid literal for int()  with base 10: "(1, 0, 'Friday')" 
monthwise = list(map(int, read_from_csv(infile_csv, month=True)[0])) .
指向该行:

monthwise = list(map(int, read_from_csv(infile_csv, month=True)[0])) .
我已经包含了示例.csv文件

monthwise = list(map(int, read_from_csv(infile_csv, month=True)[0])) .
输出:我需要的绘图,用于比较订阅者和客户之间的每月读者数

monthwise = list(map(int, read_from_csv(infile_csv, month=True)[0])) .
import calendar  
import datetime 
infile_csv = 'C:/pythonscripts/NYC-2016-Summary.csv'   
def read_from_csv(input_csvfile, duration=False, month=False, hour=False, day_of_week=False):    

# assign columns name
if duration==True:
    col_name='duration'
elif month==True:
    col_name='month'
elif hour==True:
    col_name='hour'
elif day_of_week==True:
    col_name='day_of_week'

# init lists for output
n_ridership4column = []
n_ridership_sub = []
n_ridership_cust = []

with open(infile_csv, 'r') as f_in:
    filereader = csv.DictReader(f_in)
    for row in filereader:
        n_ridership4column.append(row[col_name])
        if row['user_type'] == 'Subscriber':
            n_ridership_sub.append(row[col_name])
        else:
            n_ridership_cust.append(row[col_name])

return n_ridership4column, n_ridership_sub, n_ridership_cust

# using the function above to get monthly ridership

monthwise = list(map(int, read_from_csv(infile_csv, month=True)[0]))   
monthwise_sub = list(map(int, read_from_csv(infile_csv, month=True)[1])) 
monthwise_cust = list(map(int, read_from_csv(infile_csv, month=True)[2]))  
以下代码用于打印。这不是问题的要求,而是为了输出的清晰性

monthwise = list(map(int, read_from_csv(infile_csv, month=True)[0])) .
fig, ax = plt.subplots()
bins = [i for i in range(1,14)]                 
#upper bound is 14 to accomodate bin for december

#### Plotting monthly total along with customers and subscribers stacked

ax.hist(monthwise, bins=bins, edgecolor='k', align='left', label='Total Ridership', stacked= True)   
ax.hist(monthwise_sub, bins=bins, edgecolor='k', align='left', label='Subscribers', stacked=True)
ax.hist(monthwise_cust, bins=bins, edgecolor='k', align='left', label='Customer', stacked=True)

ax.set_xticks(bins[:-1])
ax.set_xticklabels(list(calendar.month_abbr[i] for i in bins[:-1]))


plt.title('Monthly Ridership in NYC', fontsize=16)
plt.xlabel('Monthly', fontsize=14)  
plt.ylabel('Rides', fontsize=14)
plt.xticks(fontsize=12)
plt.yticks(fontsize=12)

plt.legend()
plt.show()
这是上面代码的示例.csv文件

monthwise = list(map(int, read_from_csv(infile_csv, month=True)[0])) .
duration    month   hour    day_of_week user_type
13.98333333 (1, 0, 'Friday')        (1, 0, 'Friday')    Customer
11.43333333 (1, 0, 'Friday')        (1, 0, 'Friday')    Subscriber
5.25    (1, 0, 'Friday')        (1, 0, 'Friday')    Subscriber
12.31666667 (1, 0, 'Friday')        (1, 0, 'Friday')    Subscriber
20.88333333 (1, 0, 'Friday')        (1, 0, 'Friday')    Customer
8.75    (1, 0, 'Friday')        (1, 0, 'Friday')    Subscriber
10.98333333 (1, 0, 'Friday')        (1, 0, 'Friday')    Subscriber
7.733333333 (1, 1, 'Friday')        (1, 1, 'Friday')    Subscriber
3.433333333 (1, 1, 'Friday')        (1, 1, 'Friday')    Subscriber
7.083333333 (1, 1, 'Friday')        (1, 1, 'Friday')    Customer
13.3    (1, 2, 'Friday')        (1, 2, 'Friday')    Subscriber
9.733333333 (1, 2, 'Friday')        (1, 2, 'Friday')    Subscriber
8.416666667 (1, 2, 'Friday')        (1, 2, 'Friday')    Subscriber

错误消息表示您正在尝试将非数值的值解析为整数。当您要求Python执行它不能执行的操作(将数字除以零、引用未声明的变量等)时,它会抛出一个错误。通常错误信息非常清楚,不过当您刚刚学习Python时,有时需要使用google

monthwise = list(map(int, read_from_csv(infile_csv, month=True)[0])) .
在一定程度上,无论哪个程序编写了这个损坏的伪CSV,都是错误的,应该修复或替换。为了使CSV有用,需要将其规范化为每个字段一个基准,尽管您有时会看到这一原则被违反。以特定于Python的格式编写复合字段至少是错误的,在这种情况下很可能是一个bug

monthwise = list(map(int, read_from_csv(infile_csv, month=True)[0])) .
此外,示例数据中有一列比示例标题所显示的少。另一方面,第2列和第3列似乎总是相同的,并且模糊地显示为由符合标题中第2列、第3列和第4列的明显期望值的值组成

monthwise = list(map(int, read_from_csv(infile_csv, month=True)[0])) .
您的代码很奇怪,因为它似乎每次要提取列时都会读取文件。如果您的输入文件太大,无法一次装入内存,那么这可能会有模糊的意义;但是,如果您的问题或代码中的注释中没有任何此类问题,我建议将所有列读入内存一次。这也应该使你的程序至少快一个数量级

monthwise = list(map(int, read_from_csv(infile_csv, month=True)[0])) .
DictReader
已经负责将其输入收集到一个
orderedict
中,因此循环中的
append
只是复制这个Python库已经为您执行的工作

monthwise = list(map(int, read_from_csv(infile_csv, month=True)[0])) .
也许这样的东西会适合你的需要,如果你被困在这个破碎的CSV

monthwise = list(map(int, read_from_csv(infile_csv, month=True)[0])) .
def parse_broken_csv(filename):
    rows = []
    with open(filename, 'r') as fh:
        reader = csv.reader(fh, delimiter='\t')
        headers = reader.__next__()
        for duration, tpl, _, cust in reader:
            month, hour, dow = tpl.strip('()').split(', ')
            rows.append({
                'duration': float(duration),
                'month': int(month),
                'hour': int(hour),
                'day_of_week': dow.strip("'"),
                'cust': cust})
    return rows

rows = parse_broken_csv(r'NYC-2016-Summary.csv')

monthwise = [row['month'] for row in rows]
monthwise_sub = [row['month'] for row in rows if row['cust'] == 'Subscriber']
monthwise_cust = [row['month'] for row in rows if row['cust'] == 'Customer']
对于您发布的示例CSV,
的值为

monthwise = list(map(int, read_from_csv(infile_csv, month=True)[0])) .
[
 {'duration': 13.98333333, 'month': 1, 'day_of_week': 'Friday', 'cust': 'Customer', 'hour': 0},
 {'duration': 11.43333333, 'month': 1, 'day_of_week': 'Friday', 'cust': 'Subscriber', 'hour': 0},
 {'duration': 5.25, 'month': 1, 'day_of_week': 'Friday', 'cust': 'Subscriber', 'hour': 0},
 {'duration': 12.31666667, 'month': 1, 'day_of_week': 'Friday', 'cust': 'Subscriber', 'hour': 0},
 {'duration': 20.88333333, 'month': 1, 'day_of_week': 'Friday', 'cust': 'Customer', 'hour': 0},
 {'duration': 8.75, 'month': 1, 'day_of_week': 'Friday', 'cust': 'Subscriber', 'hour': 0},
 {'duration': 10.98333333, 'month': 1, 'day_of_week': 'Friday', 'cust': 'Subscriber', 'hour': 0}
]
[1, 1, 1, 1, 1, 1, 1]
monthwise
的值为

monthwise = list(map(int, read_from_csv(infile_csv, month=True)[0])) .
[
 {'duration': 13.98333333, 'month': 1, 'day_of_week': 'Friday', 'cust': 'Customer', 'hour': 0},
 {'duration': 11.43333333, 'month': 1, 'day_of_week': 'Friday', 'cust': 'Subscriber', 'hour': 0},
 {'duration': 5.25, 'month': 1, 'day_of_week': 'Friday', 'cust': 'Subscriber', 'hour': 0},
 {'duration': 12.31666667, 'month': 1, 'day_of_week': 'Friday', 'cust': 'Subscriber', 'hour': 0},
 {'duration': 20.88333333, 'month': 1, 'day_of_week': 'Friday', 'cust': 'Customer', 'hour': 0},
 {'duration': 8.75, 'month': 1, 'day_of_week': 'Friday', 'cust': 'Subscriber', 'hour': 0},
 {'duration': 10.98333333, 'month': 1, 'day_of_week': 'Friday', 'cust': 'Subscriber', 'hour': 0}
]
[1, 1, 1, 1, 1, 1, 1]

您正在尝试将元组转换为int…可以做些什么来更正?。我尝试添加.applystr()。它不起作用。请编辑您的问题以包含复制此错误的示例CSV文件。我在我制作的CSV文件上运行了你的代码,并且没有错误。我已经添加了我的CSV文件,还包括了用于打印的代码。我在jupyter笔记本上运行了它。Python3i试图修复格式,但是CSV示例太乱了。能否请您将样本提供在代码块中,以便复制/粘贴用于检查和实验?正确的代码格式很简单-只需粘贴代码,选择它,然后键入ctrl-K(在移动界面中,您需要手动将每个代码行缩进四个空格。)请参见当我使用上述代码运行时出现的此错误::AttributeError:“_csv.reader”对象没有属性“next”。您在Python 2上吗?尝试
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。我正在使用python 3。现在,我遇到了以下错误:ValueError:没有足够的值来解包(预期为4,得到0),这听起来像是您的解决方案消耗了整个文件,或者文件中有一个空行。