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

文本分类Python代码出现EOF语法错误的修复求助及术语解释

问题解决:Python语法错误与EOF解释

1. 错误定位与修复

你遇到的这个SyntaxError: unexpected EOF while parsing是括号不匹配导致的。在代码最后一行的return语句里,你只写了一个左括号(,却没有对应的右括号)来闭合语法结构。另外还有个容易忽略的问题:代码里用到了string.punctuation但没导入string模块,运行时也会触发报错,需要补上这个导入。

修复后的完整代码如下:

def TextClassification(a):
    import pandas as pd
    import string  # 新增:导入string模块,否则无法使用string.punctuation
    df = pd.read_excel('../Desktop/Stage/Classeur1.xlsx')
    #Removing NULL element
    df=df[pd.notnull(df['Réclamation'])]
    df=df[pd.notnull(df['Catégorie'])]
    #Removing punctuation
    df['Réclamation'] = [''.join(c for c in s if c not in string.punctuation) for s in df['Réclamation']]
    df['Catégorie'] = [''.join(c for c in s if c not in string.punctuation) for s in df['Catégorie']]
    #Removing é è and all numbers
    ch = ['0','1','2','3','4','5','6','7','8','9']
    for c in ch:
        df['Réclamation'] = [w.replace(c, '') for w in df['Réclamation']]
        df['Catégorie'] = [w.replace(c, '') for w in df['Catégorie']]
    df['Réclamation']= [w.replace('è', 'e') for w in df['Réclamation']]
    df['Réclamation']= [w.replace('é', 'e') for w in df['Réclamation']]
    df['Catégorie']= [w.replace('è', 'e') for w in df['Catégorie']]
    df['Catégorie']= [w.replace('é', 'e') for w in df['Catégorie']]
    #Lower case
    df['Réclamation']=df['Réclamation'].apply(lambda x: " ".join(x.lower() for x in x.split()))
    df['Catégorie']=df['Catégorie'].apply(lambda x: " ".join(x.lower() for x in x.split()))
    df['category_id'] = df['Catégorie'].factorize()[0]
    category_id_df = df[['Catégorie', 'category_id']].sort_values('category_id')
    category_to_id = dict(category_id_df.values)
    id_to_category = dict(category_id_df[['category_id', 'Catégorie']].values)
    #library for data preprocessing and model building
    from sklearn.model_selection import train_test_split
    from sklearn.feature_extraction.text import CountVectorizer
    from sklearn.feature_extraction.text import TfidfTransformer
    from sklearn.svm import LinearSVC
    #split data(train/test)
    X_train, X_test, y_train, y_test = train_test_split(df['Réclamation'], df['Catégorie'], random_state = 0)
    count_vect = CountVectorizer()
    X_train_counts = count_vect.fit_transform(X_train)
    tfidf_transformer = TfidfTransformer()
    X_train_tfidf = tfidf_transformer.fit_transform(X_train_counts)
    clf = LinearSVC().fit(X_train_tfidf, y_train)
    # 修复:补全缺失的右括号,同时建议直接返回预测结果而非打印后返回None
    return clf.predict(count_vect.transform(a))
    # 如果确实需要打印后再返回,可以改成:
    # result = clf.predict(count_vect.transform(a))
    # print(result)
    # return result

2. EOF的含义

EOF是End Of File的缩写,翻译成中文是“文件结束符”。

在这个语法错误里,unexpected EOF while parsing的意思是:Python解释器在解析你的代码时,还没完成当前语法结构的解析(比如这里未闭合的括号),就已经读到了代码的结尾。简单来说就是代码的语法结构不完整,解释器找不到能闭合当前代码块/表达式的符号,所以触发了报错。

常见导致这类错误的场景包括:

  • 括号、引号没有成对闭合
  • 缩进的代码块(比如if、for、def)没有写完就结束了
  • 多行字符串没有正确闭合

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.11 08:51:39