Where and how do I strip the blank lines created by google docs on the Drive API?

拈花ヽ惹草 提交于 2019-12-11 15:12:01

问题


It appears that somewhere in the process of updating or downloading files from Google Docs using their API, Google adds one new line for every one of your blank lines. See: Google Drive API on upload--where are these extra blank lines coming from?

I'm trying to figure out how to get rid of the new lines created by Google while keeping my own intentionally placed new lines.

I'm finding it difficult to target when exactly it's occurring, in order to strip the \r or \n.

I thought it may occur during download, but I can't seem to get this file to print anything:

def download_file(service, file_id, filepath):
    request = service.files().export_media(fileId=file_id, mimeType='text/plain')
    fh = io.BytesIO()
    downloader = MediaIoBaseDownload(fh, request)
    done = False
    while done is False:
        status, done = downloader.next_chunk()
        print("Download %d%%." % int(status.progress() * 100))
    with io.open(filepath,'wb') as f:
        fh.seek(0)
        f.write(fh.read())
        # mylist = f.read()

        # final = [line.rstrip('\n') for line in f]  

When I'm writing to the file that will eventually be uploaded on the Drive, print show's no indication that there are any rogue new lines. Things look the way they should be:

    with open("file_b.txt", "a+") as f:
        readfile = f.readlines()
        # readfile = [line.rstrip('\n') for line in f]
        # readfile = readfile.splitlines()
        print("read", readfile)
        # readfile.remove('\n')
        list2 = [a for a in readfile if a != '\n']
        print("newread", list2)
        # for line in readfile:
        #     print(line)
        #     line.replace('\n', '')
        # print("replaced", readfile)
        # readfile.
        # mylist = [line.rstrip('\n') for line in myfile]
        # print(mylist)
        list2.extend(data)

        print("2", list2)
        for item in list2:
            f.write(item)  

nothing seems to work.

How do I strip these ghost lines from Google Drive?

来源:https://stackoverflow.com/questions/49625386/where-and-how-do-i-strip-the-blank-lines-created-by-google-docs-on-the-drive-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!