Read excel file get TypeError:int()参数必须是字符串,而不是字符串中的'\u NoValueType'

2024-04-16 06:02:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我用df = pd.read_csv(encoding = 'utf-8', engine = 'python')读取了一个excel文件,而print(df)我得到了以下类型错误:

Traceback (most recent call last):

  File "C:\Users\User\Anaconda3\lib\site-packages\IPython\core\formatters.py", line 702, in __call__
    printer.pretty(obj)

  File "C:\Users\User\Anaconda3\lib\site-packages\IPython\lib\pretty.py", line 400, in pretty
    return _repr_pprint(obj, self, cycle)

  File "C:\Users\User\Anaconda3\lib\site-packages\IPython\lib\pretty.py", line 695, in _repr_pprint
    output = repr(obj)

  File "C:\Users\User\Anaconda3\lib\site-packages\pandas\core\base.py", line 82, in __repr__
    return str(self)

  File "C:\Users\User\Anaconda3\lib\site-packages\pandas\core\base.py", line 61, in __str__
    return self.__unicode__()

  File "C:\Users\User\Anaconda3\lib\site-packages\pandas\core\frame.py", line 663, in __unicode__
    line_width=width, show_dimensions=show_dimensions)

  File "C:\Users\User\Anaconda3\lib\site-packages\pandas\core\frame.py", line 1971, in to_string
    formatter.to_string()

  File "C:\Users\User\Anaconda3\lib\site-packages\pandas\io\formats\format.py", line 620, in to_string
    max_len = Series(text).str.len().max()

  File "C:\Users\User\Anaconda3\lib\site-packages\pandas\core\generic.py", line 9611, in stat_func
    numeric_only=numeric_only)

  File "C:\Users\User\Anaconda3\lib\site-packages\pandas\core\series.py", line 3221, in _reduce
    return op(delegate, skipna=skipna, **kwds)

  File "C:\Users\User\Anaconda3\lib\site-packages\pandas\core\nanops.py", line 131, in f
    result = alt(values, axis=axis, skipna=skipna, **kwds)

  File "C:\Users\User\Anaconda3\lib\site-packages\pandas\core\nanops.py", line 507, in reduction
    result = getattr(values, meth)(axis)

  File "C:\Users\User\Anaconda3\lib\site-packages\numpy\core\_methods.py", line 28, in _amax
    return umr_maximum(a, axis, None, out, keepdims, initial)

TypeError: int() argument must be a string, a bytes-like object or a number, not '_NoValueType'

当读取列名时,我得到Index(['names\t', 'matched_names', 'ratio'], dtype='object')。你知道吗

如果有人知道是什么导致了这个打字错误,请帮助我。提前谢谢。你知道吗

更新:为了您更好地理解,我添加了如何生成此csv文件的代码:

with open('result.csv', 'w', encoding = 'utf_8_sig') as f1:
    writer = csv.writer(f1, delimiter='\t', lineterminator='\n', )
    writer.writerow(('names', 'matched_names', 'ratio'))
    for dish1, dish2 in itertools.combinations(enumerate(processedDishes), 2):
        matcher = matchers[dish1[0]]
        matcher.set_seq2(dish2[1])
        ratio = int(round(100 * matcher.ratio()))
        if ratio >= threshold_ratio:
            #print(dishes[dish1[0]], dishes[dish2[0]])
            my_list = (dishes[dish1[0]], dishes[dish2[0]], ratio)
            print(my_list)
            writer.writerow([my_list])

my_list有如下格式的行:('steve', 'john', 0)

df.info()

Traceback (most recent call last):

  File "<ipython-input-142-83941e9879da>", line 1, in <module>
    df.info()

  File "C:\Users\User\Anaconda3\lib\site-packages\pandas\core\frame.py", line 2252, in info
    _verbose_repr()

  File "C:\Users\User\Anaconda3\lib\site-packages\pandas\core\frame.py", line 2210, in _verbose_repr
    counts = self.count()

  File "C:\Users\User\Anaconda3\lib\site-packages\pandas\core\frame.py", line 6800, in count
    result = Series(counts, index=frame._get_agg_axis(axis))

  File "C:\Users\User\Anaconda3\lib\site-packages\pandas\core\series.py", line 262, in __init__
    .format(val=len(data), ind=len(index)))

ValueError: Length of passed values is 0, index implies 3

Tags: inpycorepandaslibpackageslinesite