Read .csv file from URL into Python 3.x - _csv.Error: iterator should return strings, not bytes (did you open the file in text mode?)

后端 未结 4 1792
傲寒
傲寒 2020-12-01 03:28

I\'ve been struggling with this simple problem for too long, so I thought I\'d ask for help. I am trying to read a list of journal articles from National Library of Medicine

4条回答
  •  一整个雨季
    2020-12-01 04:15

    I had a similar problem using requests package and csv. The response from post request was type bytes. In order to user csv library, first I a stored them as a string file in memory (in my case the size was small), decoded utf-8.

    import io
    import csv
    import requests
    
    response = requests.post(url, data)
    
    # response.content is something like: 
    # b'"City","Awb","Total"\r\n"Bucuresti","6733338850003","32.57"\r\n'    
    csv_bytes = response.content
    
    # write in-memory string file from bytes, decoded (utf-8)
    str_file = io.StringIO(csv_bytes.decode('utf-8'), newline='\n')
        
    reader = csv.reader(str_file)
    for row_list in reader:
        print(row_list)
    
    # Once the file is closed,
    # any operation on the file (e.g. reading or writing) will raise a ValueError
    str_file.close()
    

    Printed something like:

    ['City', 'Awb', 'Total']
    ['Bucuresti', '6733338850003', '32.57']
    

提交回复
热议问题