简体   繁体   English

在 csv.reader 中访问数据

[英]Accessing Data in csv.reader

I'm trying to access a csv file of currency pairs using csv.reader.我正在尝试使用 csv.reader 访问货币对的 csv 文件。 The first column shows dates, the first row shows the currency pair eg.USD/CAD.第一列显示日期,第一行显示货币对,例如美元/加元。 I can read in the file but cannot access the currency pairs data to perform simple calculations.我可以读入文件,但无法访问货币对数据来执行简单的计算。

I've tried using next(x) to skip header row (currency pairs).我试过使用 next(x) 跳过标题行(货币对)。 If i do this, i get a Typeerror: csv reader is not subscriptable.如果我这样做,我会收到一个 Typeerror: csv reader is not subscriptable。

path = x
file = open(path)
dataset = csv.reader(file, delimiter = '\t',)
header = next(dataset)
header

Output shows the header row which is输出显示标题行

['Date,USD,Index,CNY,JPY,EUR,KRW,GBP,SGD,INR,THB,NZD,TWD,MYR,IDR,VND,AED,PGK,HKD,CAD,CHF,SEK,SDR']

I expect to be able to access the underlying currency pairs but i'm getting the type error as noted above.我希望能够访问基础货币对,但我收到了如上所述的类型错误。 Is there a simple way to access the currency pairs, for example I want to use USD.describe() to get simple statistics on the USD currency pair.是否有一种简单的方法来访问货币对,例如我想使用 USD.describe() 来获取有关美元货币对的简单统计信息。

How can i move from this stage to accessing the data underlying the header row?我怎样才能从这个阶段转移到访问标题行下面的数据?

try this example试试这个例子

import csv

with open('file.csv') as csv_file:
    csv_reader = csv.Reader(csv_file, delimiter='\t')
    line_count = 0
    for row in csv_reader:
        print(f'\t{row[0]} {row[1]} {row[3]}')

从标题行的输出中可以明显看出,列是逗号分隔而不是制表符分隔的,因此不应将delimiter = '\\t'传递给csv.reader ,而应让它使用默认分隔符','

dataset = csv.reader(file)

Are you sure that your delimiter is '\\t' ?您确定您的分隔符是'\\t'吗? In first row your delimiter is ',' ... Anyway you can skip first row by doing file.readline() before using it by csv.reader :在第一排的分隔符是',' ......反正你可以做跳过第一行file.readline()使用它通过前csv.reader

import csv

example = """Date,USD,Index,CNY,JPY,EUR,KRW,GBP,SGD,INR,THB,NZD,TWD,MYR,IDR,VND,AED,PGK,HKD,CAD,CHF,SEK,SDR
1-2-3\tabc\t1.1\t1.2
4-5-6\txyz\t2.1\t2.2
"""

with open('demo.csv', 'w') as f:
    f.write(example)

with open('demo.csv') as f:
    f.readline()
    reader = csv.reader(f, delimiter='\t')
    for row in reader:
        print(row)
# ['1-2-3', 'abc', '1.1', '1.2']                                                                                                                                                                                                   
# ['4-5-6', 'xyz', '2.1', '2.2']

I think that you need something else... Can you add to your question:我认为你需要别的东西......你能补充一下你的问题吗:

  • example of first 3 lines in your csv csv 中前 3 行的示例
  • Example of what you'd like to access:您想要访问的示例:

    • is using row[0] , row[1] enough for you?使用row[0] , row[1]对你来说足够了吗?
    • or do you want "named" access like row['Date'], row['USD'] ,或者你想要像row['Date'], row['USD']这样的“命名”访问,
    • or you want something more complex like data_by_date['2019-05-01']['USD']或者你想要更复杂的东西,比如data_by_date['2019-05-01']['USD']

If you need to elaborate some statistics pandas is your friend.如果您需要详细说明一些统计数据, pandas是您的朋友。 No need to use the csv module, use pandas.read_csv .不需要使用csv模块,使用pandas.read_csv

import pandas

filename = 'path/of/file.csv'
dataset = pandas.read_csv(filename, sep = '\t') #or whatever the separator is

pandas.read_csv uses the first line as the header automatically. pandas.read_csv使用第一行作为标题。
To see statistics, simply do:要查看统计数据,只需执行以下操作:

dataset.describe()

Or for a single column:或者对于单列:

dataset['column_name'].describe()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM