You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

从Google Drive导入txt文件至Google Colab出现No columns to parse错误如何解决

错误原因排查与解决方案

错误触发原因

  • 你贴出的3行挂载谷歌云盘、读取文件的代码本身不会触发No columns to parse from file报错,该报错为pandas读取结构化文件的专属报错,说明你后续使用了pd.read_csv()/pd.read_table()等pandas读取方法处理该txt文件时触发了错误。
  • 你的txt文件仅包含0/1纯文本内容,无表头行、无固定列分隔符,pandas默认读取规则找不到可解析的列定义,就会抛出该错误。
  • 额外排查点:如果确认路径正确的情况下,先验证文件内容是否为空、是否前N行全是空行,也会导致pandas无法识别列。

对应解决方案

场景1:仅需提取所有0/1内容做非结构化处理

你已有的open读取逻辑完全可用,仅需对读取到的字符串做简单清洗即可:

from google.colab import drive
drive.mount('/content/drive')

# 读取并清洗内容
with open('/content/drive/MyDrive/Data/Graph2021.txt', 'r', encoding='utf-8') as f:
    content = f.read()
# 去掉换行符、空白字符,提取所有0/1转为整数列表
num_list = [int(c) for c in content if c in ('0', '1')]

场景2:需要将内容转为结构化的DataFrame表格

手动逐行读取拆分,避免pandas默认规则匹配失败:

import pandas as pd

with open('/content/drive/MyDrive/Data/Graph2021.txt', 'r', encoding='utf-8') as f:
    # 逐行读取,去掉空行,将每行的单个字符拆为列表元素
    rows = [list(line.strip()) for line in f if line.strip()]
# 转为DataFrame,自动生成列名,内容转为整数类型
df = pd.DataFrame(rows, dtype=int)

前置验证步骤

如果按上述方案还是报错,可先执行以下代码确认文件内容是否正常:

with open('/content/drive/MyDrive/Data/Graph2021.txt', 'r', encoding='utf-8') as f:
    # 打印前5行的原始内容,排查是否为空、是否有异常字符
    for i in range(5):
        print(f"第{i+1}行:{repr(f.readline())}")

内容的提问来源于stack exchange,提问作者ppotatomato

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 15:09:02