【问题标题】:i keep getting this error and i don't know why我不断收到此错误,我不知道为什么
【发布时间】:2020-01-06 01:33:53
【问题描述】:

这是错误

[Running] python -u "/Users/thomasmciver/Desktop/chat/ai-chat.py"
  File "/Users/thomasmciver/Desktop/chat/ai-chat.py", line 38
    with open ('Users/thomasmciver/Desktop/chat/RC_2015-01.txt⁩' buffering=1000) as f:
                                                                           ^
SyntaxError: invalid syntax

[Done] exited with code=1 in 0.051 seconds

这是我的代码

# This Python file uses the following encoding: utf-8
import sqlite3
import json
from datetime import datetime

timeframe = '2015-05'
sql_transaction = []

connection = sqlite3.connect('{}.db'.format(timeframe))
c = connection.cursor()

def create_table():
    c.execute("CREATE TABLE IF NOT EXISTS parent_reply(parent_id TEXT PRIMARY KEY, comment_id TEXT UNIQUE, parent TEXT, comment TEXT, subreddit TEXT, unix INT, score INT)")

def format_data(data):
    data = data.replace('\n',' newlinechar ').replace('\r',' newlinechar ').replace('"',"'")
    return data

def find_parent(pid):
    try:
        sql = "SELECT comment FROM parent_reply WHERE comment_id = '{}' LIMIT 1".format(pid)
        c.execute(sql)
        result = c.fetchone()
        if result != None:
            return result[0]
        else: return False
    except Exception as e:
        #print(str(e))
        return False


if __name__ == '__main__':
    create_table()
    row_counter = 0
    paired_rows = 0


    with open ('Users/thomasmciver/Desktop/chat/RC_2015-01.txt⁩' buffering=1000) as f:
        for row in f:
            row_counter += 1
            row = json.loads(row)
            parent_id = row['parent_id']
            body = format_data(row['body'])
            created_utc = row['created_utc']
            score = row['score']
            comment_id = row['name']
            subreddit = row['subreddit']
            parent_data = find_parent(parent_id)

            if __name__ == '__main__':
                create_table()
                row_counter = 0
                paired_rows = 0
            # maybe check for a child, if child, is our new score superior? If so, replace. If not...

            if score >= 2:
                existing_comment_score = find_existing_score(parent_id)

【问题讨论】:

  • except Exception as e: 是不好的做法。您应该使用is None 或is not None 测试变量是否为None,而不是==。请修复代码的格式,我什至不清楚问题是堆栈溢出的格式还是程序本身。

标签: python python-3.x artificial-intelligence chatbot


【解决方案1】:

你是说

with open('Cloud Drive⁩/Desktop/chat/RC_2015-01.txt⁩', buffering=1000) as f:

而不是

with open ('Cloud Drive⁩/Desktop/chat/RC_2015-01.txt⁩' (buffering=1000)) as f:

您所拥有的语法无效。

【讨论】:

  • 现在我得到了这个 [Running] python -u "/Users/thomasmciver/Desktop/chat/ai-chat.py" File "/Users/thomasmciver/Desktop/chat/ai-chat.py ",第 38 行,打开 ('Users/thomasmciver/Desktop/chat/RC_2015-01.txt' buffering=1000) 作为 f: ^ SyntaxError: invalid syntax [Done] exited with code=1 in 0.051 seconds
  • @mciver9010 您在字符串之后和buffering 之前缺少逗号。完全按照答案中的示例进行操作,您应该会得到它。
  • 现在它的说法 [Running] python -u "/Users/thomasmciver/Desktop/chat/ai-chat.py" Traceback(最近一次通话最后):文件 "/Users/thomasmciver/Desktop/ chat/ai-chat.py",第 38 行,在 中打开 ('Users/thomasmciver/Desktop/chat/RC_2015-01.txt', buffering=1000) as f: IOError: [Errno 2] No这样的文件或目录: 'Users/thomasmciver/Desktop/chat/RC_2015-01.txt\xe2\x81\xa9' [Done] exited with code=1 in 0.084 seconds
  • @mciver9010,在您解决所有错误之前,我们不会在这里牵着您的手。您提出了一个问题并得到了似乎有用的答案。这应该是它的结束。见stackoverflow.com/help/someone-answers。仔细阅读您的新错误消息,看看您是否能找出问题所在。 (提示:这个说找不到你的文件。仔细看看它要找的路径。它看起来对吗?)如果你自己无法弄清楚,搜索现有的类似问题。如果那不这样做,请随意提出一个新问题。请收下tour 并阅读How to Ask。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-10
  • 2023-01-02
  • 1970-01-01
  • 1970-01-01
  • 2017-05-07
  • 2017-11-23
  • 2019-02-14
相关资源
最近更新 更多