在Python 3中在线读取CSV文件中的数据

时间:2014-01-25 14:37:16

标签: python csv

只是作为练习测试一些东西。我在线有这个巨大的CSV文件 https://raw.github.com/datasets/gdp/master/data/gdp.csv 我想读取所有数据并将其放入表中,以便我可以分析它并制作表格。 我到目前为止的代码是使用其他StackOverflow问题和其他网站放在一起的,但似乎发生的事情是什么时候它被阅读,然后立即打印出来的是它的字母,所以我得到:

['C']
['o']
['u']
['n']
['t']
['r']
['y']
[' ']
['N']
['a']
['m']
['e']
['', '']
['C']
['o']
['u']
['n']
['t']
['r']
['y']
[' ']
['C']
['o']
['d']
['e']
['', '']
['Y']
['e']
['a']
['r']
['', '']
['V']
['a']
['l']
['u']
['e']
[]
[]
['A']
['r']
['a']
['b']
[' ']
['W']
['o']
['r']
['l']
['d']
['', '']

到目前为止我的代码是这样的:

import csv
import urllib.request

url = "https://raw.github.com/datasets/gdp/master/data/gdp.csv"
webpage = urllib.request.urlopen(url)
datareader = csv.reader(webpage.read().decode('utf-8'))
data = []
for row in datareader:
    data.append(row)

for row in data:
    print(row)

如何更改它以使其实际逐行读取,然后甚至将该行拆分为不同的变量。我在使用

之前做了这个
payRollNumber, salary, jobTitle, otherNames, \
               surname = line.strip().split(',')

我可以在获得行后应用它。有什么想法吗?

2 个答案:

答案 0 :(得分:14)

在将读取的CSV数据传递给csv.reader()

之前,您需要按行拆分读取的CSV数据
datareader = csv.reader(webpage.read().decode('utf-8').splitlines())

csv.reader()然后为你处理剩下的事情。

您还可以io.TextIOWrapper()为您处理阅读,解码和线路处理:

import csv
import io
import urllib.request

url = "https://raw.github.com/datasets/gdp/master/data/gdp.csv"
webpage = urllib.request.urlopen(url)
datareader = csv.reader(io.TextIOWrapper(webpage))

循环读取器并向列表中添加行几乎没有意义;你可以这么做:

data = list(datareader)

相反,但如果您只想打印出列,请直接在阅读器上循环并执行此操作:

datareader = csv.reader(io.TextIOWrapper(webpage))
for row in datareader:
    print(row)

无论哪种方式,通过自己分割线条或使用TextIOWrapper,代码现在产生:

['Country Name', 'Country Code', 'Year', 'Value']
['Arab World', 'ARB', '1968', '32456179321.45']
['Arab World', 'ARB', '1969', '35797666653.6002']
['Arab World', 'ARB', '1970', '39062044200.4362']
['Arab World', 'ARB', '1971', '45271917893.3429']
['Arab World', 'ARB', '1972', '54936622019.8224']
['Arab World', 'ARB', '1973', '69564884441.8264']
['Arab World', 'ARB', '1974', '132123836511.468']
['Arab World', 'ARB', '1975', '147666389454.913']
['Arab World', 'ARB', '1976', '182208407088.856']
# ... etc. ...

答案 1 :(得分:0)

处理 CSV 的更好方法。

您可以根据您的方便使用 csv readerDictReader

CSV Reader

<块引用>

['国家名称'、'国家代码'、'年份'、'值'] ['阿拉伯世界'、'ARB'、'1968'、'32456179321.45']... ...

DictReader

<块引用>

{"Country Name": 'Arab World', 'Country Code': 'ARB', 'Year':'1968', 'Value':'32456179321.45'} ... ...

import requests

download = requests.get(CSV_URL)
decoded_content = download.content.decode('utf-8')
file = decoded_content.splitlines()

# cr = csv.reader(file, delimiter=',')
cr = csv.DictReader(file, delimiter=',')
my_list = list(cr)
for row in my_list:
    print(row)