我有一个python代码从推文中提取功能。所以我必须将我的推文文件作为输入及其路径。这该怎么做?

时间:2018-04-08 11:22:26

标签: python scikit-learn

def main():

init_dicts()  #initialize the dictionaries 

trainingFile = open("output.csv",'a')
wr = csv.writer(trainingFile, quoting=csv.QUOTE_ALL)

path_sarcastic = os.path.abspath(in.txt + "C:\Desktop/miniproject/project_test") + "/sarcastic_with_past"
fileListSarcastic = os.listdir(path_sarcastic)
for i in fileListSarcastic:
    list_tweets = []
    features = []
    with open(path_sarcastic+'/'+i) as tweet_file:
        file_reader = csv.DictReader(tweet_file)
        for row in file_reader:
                try:
                    words = preprocess(row['tweet'],stopwords)
                    list_tweets.append(words)
                except:
                    print "problem"
    words = list_tweets[0]
    past = list_tweets[1:]
    features = getFeatureHelper(words , past)
    features.append(1)
    wr.writerow(features) 

我的错误是:

  

〜/ Desktop / miniproject / project_test $ python driver_final.py文件   “driver_final.py”,第93行       path_sarcastic = os.path.abspath(in.txt +“C:\ Desktop / miniproject / project_test”)+“/ sarcastic_with_past”                                         ^ SyntaxError:语法无效

有人请帮我找到问题吗?

1 个答案:

答案 0 :(得分:0)

问题是: 这是制作绝对路径的错误方法,这就是您看到此语法错误的原因:

  

os.path.abspath(in.txt +“C:\ Desktop / miniproject / project_test”)+   “/ sarcastic_with_past”

以下是您需要做的事情:

替换:

path_sarcastic = os.path.abspath(in.txt + "C:\Desktop/miniproject/project_test") + "/sarcastic_with_past"

with:

path_sarcastic = os.path.abspath("C:\Desktop/miniproject/project_test"+"/sarcastic_with_past"+"/in.txt")