简体   繁体   English

python过滤到csv.reader的输入

[英]python filtered input to csv.reader

My program reads a csv file but recently the input file was changed to be base64 encoded. 我的程序读取了一个csv文件,但最近输入文件已更改为base64编码。 So currently the read code is: 因此,当前读取的代码是:

with open(uploadFile, 'rb') as csvfile:
    spreadSheet = csv.reader(csvfile, delimiter=',')

I know the csv is a file descriptor and this can't be done, but I want to do something like: 我知道csv是一个文件描述符,无法完成,但是我想执行以下操作:

import base64
with open(uploadFile, 'rb') as csvfile:
   spreadSheet = csv.reader(bas64.decode(csvfile), delimiter=',')

That is the file input would be base64 decoded as though in a pipe and then parsed as a csv file. 也就是说,文件输入将像在管道中一样被base64解码,然后解析为csv文件。

I can read the file decode it write back into another file and then read that file with the csv reader but that all seems as though there should be a way to do it as a pipe sequence. 我可以读取文件并将其解码,然后写回另一个文件,然后使用csv阅读器读取该文件,但是似乎所有事情似乎都应该有一种方法来作为管道序列来完成。

Try the following 尝试以下

import base64
import csv

with open(uploadFile, 'rb') as csvfile:
    decoded = base64.standard_b64decode(csvfile.read()).decode('utf-8')
    spreadSheet = csv.reader(decoded.splitlines(), delimiter=',')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM