Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何修复python代码中的语法错误?_Python_Csv_Syntax Error - Fatal编程技术网

如何修复python代码中的语法错误?

如何修复python代码中的语法错误?,python,csv,syntax-error,Python,Csv,Syntax Error,我将从网站上学习本教程: 到目前为止,一切都很好,但我在尝试运行此代码时不断遇到错误 def buildTrainingSet(corpusFile, tweetDataFile): import csv import time corpus = [] with open(corpusFile,'rb') as csvfile: lineReader = csv.reader(csvfile,delimiter=',', quotechar="\"") for row in

我将从网站上学习本教程: 到目前为止,一切都很好,但我在尝试运行此代码时不断遇到错误

def buildTrainingSet(corpusFile, tweetDataFile):
import csv
import time

corpus = []

with open(corpusFile,'rb') as csvfile:
    lineReader = csv.reader(csvfile,delimiter=',', quotechar="\"")
    for row in lineReader:
        corpus.append({"tweet_id":row[2], "label":row[1], "topic":row[0]})

rate_limit = 180
sleep_time = 900/180

trainingDataSet = []

for tweet in corpus:
    try:
        status = twitter_api.GetStatus(tweet["tweet_id"])
        print("Tweet fetched" + status.text)
        tweet["text"] = status.text
        trainingDataSet.append(tweet)
        time.sleep(sleep_time) 
    except: 
        continue
# now we write them to the empty CSV file
with open(tweetDataFile,'wb') as csvfile:
    linewriter = csv.writer(csvfile,delimiter=',',quotechar="\"")
    for tweet in trainingDataSet:
        try:
            linewriter.writerow([tweet["tweet_id"], tweet["text"], tweet["label"], tweet["topic"]])
        except Exception as e:
            print(e)
return trainingDataSet
 #================
corpusFile = "C:\Users\Vilma\Documents\CIS450\group prjt/corpus.csv"
tweetDataFile = "C:\Users\Vilma\Documents\CIS450\group prjt/tweetDataFile.csv"

trainingData = buildTrainingSet (corpusFile, tweetDataFile)
我不断地发现这个错误:

 File "<ipython-input-33-54fea359e8f9>", line 1
    corpusFile = "C:\Users\Vilma\Documents\CIS450\group prjt/corpus.csv"
                ^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
但是,会弹出一个新错误:

File "<ipython-input-41-f44768dabc6e>", line 7, in buildTrainingSet
    with open(corpusFile,'rb') as csvfile:

FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\Vilma\\Documents\\CIS450\\group prjt\\corpus.csv'
BuildingSet中第7行的文件“” 以csvfile形式打开(corpusFile,'rb'): FileNotFoundError:[Errno 2]没有这样的文件或目录:“C:\\Users\\Vilma\\Documents\\CIS450\\group prjt\\corpus.csv”
尝试更正文件路径

corpusFile = "C:\Users\Vilma\Documents\CIS450\group prjt/corpus.csv"

    Should be:

    corpusFile = "C:\Users\Vilma\Documents\CIS450\group prjt\corpus.csv"
希望这有帮助

您可以使用:

corpusFile=r“C:\Users\Vilma\Documents\CIS450\group prjt\corpus.csv”


如果找不到该文件,请确保文件夹中存在该文件。

Hey!欢迎使用StackOverFlow:)您介意解释一下为什么切换斜杠类型吗?一般来说,如果你能解释你为什么这么做,以及为什么你认为这样做有效,答案会更有帮助!我注意到文件路径中有一个错误,因为corpus.csv之前的斜杠与其他斜杠不同。通常,文件路径不会使用两个不同的斜杠。也许这篇文章会有所帮助:。我知道你已经使用了“r”选项,但这篇文章提到的不止这些,它可能值得一看!副本
corpusFile = "C:\Users\Vilma\Documents\CIS450\group prjt/corpus.csv"

    Should be:

    corpusFile = "C:\Users\Vilma\Documents\CIS450\group prjt\corpus.csv"