Where and how do I strip the blank lines created b

2019-08-23 10:43发布

问题:

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?