Google drive api V3从驱动器读取.txt文件

问题描述 投票:1回答:1

我正在尝试从Google云端硬盘读取.txt文件并从中获取内容。我已经能够弄清楚如何上传和读取文件,但是,我在从文件中提取内容并将其放入字典时遇到了麻烦。有任何想法吗?提前致谢。我正在尝试为不和谐的机器人执行此操作,并将.txt数据文件存储在我的Google驱动器中。

这里是我到目前为止的代码。

# retrieve the file from the API.
file_ap_txt = service.files().get(fileId=driveFiles['ap.txt']).execute()
print(file_ap_txt)
#service.files().delete(fileId=driveFiles['ap.txt']).execute()

@client.event
async def on_ready():
    print('We have logged in as {0.user}'.format(client))


def dictRead(fileName, dictionary, separator):
    global file_ap_txt
    with open(file_ap_txt.name, 'r') as reader:
        line = reader.readline()
        while line != '':
            try:
                print(line, end='')
                lineTuple = line.partition(separator)
                dictionary[int(lineTuple[0])] = lineTuple[2]
                counter = int(lineTuple[0])
                counterVal = lineTuple[2]
                line = reader.readline()
            except:
                dictionary[counter] += line
                line = reader.readline()

我正在按如下方式存储数据:

0 | US History
1 | History of Art
2 | Art: Studio Art - Drawing Portfolio
3 | Art: Studio Art - 2-D Design
4 | Art: Studio Art - 3-D Design
5 | Biology
6 | Chemistry
7 | Chinese
8 | Computer Science A
9 | Computer Science Principles
10 | Computer Science AB
11 | Economics: Microeconomics
12 | Economics: Macroeconomics
13 | English Language & Composition
14 | English Literature & Composition
15 | Environmental Science
16 | European History
17 | French Language
18 | French Literature
19 | Human Geography
20 | German Language
21 | Government & Politics: United States
22 | Government & Politics: Comparative
23 | Latin
24 | Japanese Language & Culture
25 | Math: Calculus AB
26 | Math: Calculus BC
27 | Music Theory: Aural Subscore
28 | Music Theory: Non-Aural Subscore
29 | Physics B
30 | Physics C: Mechanics
31 | Physics C: Electricity & Magnetism
32 | Physics 1
33 | Physics 2
34 | Psychology
35 | Spanish Language
36 | Spanish Literature
37 | Statistics
38 | World History
python google-drive-api
1个回答
0
投票
© www.soinside.com 2019 - 2024. All rights reserved.