我正在尝试提取文本响应,并希望将其写入AWS S3中的CSV文件。下面的代码可以正常工作,但是存在性能问题(问题与Split函数有关)。还有其他方法可以做到这一点吗?
response = requests.get(URL,headers = {'Authorization': token})
if response.status_code == 200:
if(len(response.text) > 0):
if(count > 0):
data = response.text.split("\n")[1:-1]
else:
data = response.text.split("\n")[:-1]
if data and (len(data) > 0):
if(count == 0):
fout = smart_open.smart_open(S3path, 'wb')
for row in data:
fout.write(str.encode(row))
fout.write(b'\n')
示例响应如下:
response.text = header1,header2,header3,header4 \ nmango,graps,milk,chicken \ n俄罗斯,英格兰,美国,''\ nKG,gram,'',''\ n