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

问题描述:

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

Just testing something out as practice. I have this huge CSV file online https://raw.github.com/datasets/gdp/master/data/gdp.csv And I want to read all the data and put it into a table so I can do analyse it and make tables. The code I have so far was put together using other * questions and other websites but what seems to happen is when it's read, and then immediately printed out again it's letter by letter so I get:

['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)

如何更改它,以便它实际上逐行读取,甚至将该行分成不同的变量。我在使用之前这样做了

How can I change it so that it actually reads line by line and then even splits the line up into different variables. I did this before using

payRollNumber, salary, jobTitle, otherNames, \
               surname = line.strip().split(',')

任何想法?

您需要先将读取的CSV数据分行,然后传递给 csv .reader()

You need to split the read CSV data by lines before passing it to the csv.reader():

datareader = csv.reader(webpage.read().decode('utf-8').splitlines())

csv.reader

您还可以拥有 io.TextIOWrapper() 照顾读取,解码和线 - 为您处理:

You could also have io.TextIOWrapper() take care of reading, decoding and line-handling for you:

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))

循环阅读器和添加行到列表;你可以这样做:

There is little point in looping over the reader and adding rows to a list; you could just do:

data = list(datareader)

,但如果你想做的就是打印出列,直接在阅读器上循环,这样做:

instead, but if all you want to do is print out the columns, loop directly over the reader and do so:

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

无论如何,通过自己拆分线或使用 TextIOWrapper

Either way, with splitting the lines yourself or using TextIOWrapper, the code now produces:

['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. ...