2017-06-02 117 views
0

我從S3使用boto3csv文件,並希望訪問csv的特定列。我有這樣的代碼,我讀csv文件轉換成S3對象使用boto3但我有在訪問特定列出來的麻煩:問題訪問解讀爲S3對象CSV文件中的特定列與boto3

import boto3 

s3 = boto3.resource('s3',aws_access_key_id = keyId, aws_secret_access_key = sKeyId) 

obj = s3.Object(bucketName, srcFileName) 

filedata = obj.get()["Body"].read() 
print(filedata.decode('utf8')) 

for row in filedata.decode('utf8'): 
    print(row[1]) # Get the column at index 1 

當我執行這個上面print(filedata.decode('utf8'))打印以下我的輸出控制檯上:

51350612,Gary Scott 
10100063,Justin Smith 
10100162,Annie Smith 
10100175,Lisa Shaw 
10100461,Ricardo Taylor 
10100874,Ricky Boyd 
10103593,Hyman Cordero 

但行內print(row[1])循環for拋出誤差IndexError: string index out of range

如何刪除這個錯誤和訪問特定的列走出S3使用`boto3 csv文件嗎?

回答

0

從CSV中正確讀取,導入CSV Python模塊,並使用它的讀者之一。

文檔:https://docs.python.org/2/library/csv.html

+0

我知道如何讀取csv文件。問題是使用'boto3'包從amazon s3讀取一個csv文件,然後訪問一個列出來的錯誤列 – user2966197

1

boto3.s3.get()閱讀()將獲取整個文件的字節對象。您的代碼filedata.decode('utf8')僅將整個字節對象轉換爲String對象。這裏沒有解析發生。這是從另一個答案from another answer無恥的副本。

import csv 
# ...... code snipped .... insert your boto3 code here 

# Parse your file correctly 
lines = response[u'Body'].read().splitlines() 
# now iterate over those lines 
for row in csv.DictReader(lines): 
    # here you get a sequence of dicts 
    # do whatever you want with each line here 
    print(row) 

如果你只是有一個簡單的CSV文件,一個快速和骯髒的修復會做

for row in filedata.decode('utf8').splitlines(): 
    items = row.split(',') 
    print(items[0]. items[1]) 

How do I read a csv stored in S3 with csv.DictReader?