Python 在 csv 文件中单独读取列名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28836781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading column names alone in a csv file
提问by Tania
I have a csv file with the following columns:
我有一个包含以下列的 csv 文件:
id,name,age,sex
身、姓名、年龄、性别
Followed by a lot of values for the above columns. I am trying to read the column names alone and put them inside a list.
后面是上面列的很多值。我试图单独阅读列名并将它们放入列表中。
I am using Dictreader and this gives out the correct details:
我正在使用 Dictreader,这给出了正确的细节:
with open('details.csv') as csvfile:
i=["name","age","sex"]
re=csv.DictReader(csvfile)
for row in re:
for x in i:
print row[x]
But what I want to do is, I need the list of columns, ("i" in the above case)to be automatically parsed with the input csv than hardcoding them inside a list.
但是我想要做的是,我需要使用输入 csv 自动解析列列表(在上述情况下为“i”),而不是将它们硬编码在列表中。
with open('details.csv') as csvfile:
rows=iter(csv.reader(csvfile)).next()
header=rows[1:]
re=csv.DictReader(csvfile)
for row in re:
print row
for x in header:
print row[x]
This gives out an error
这给出了一个错误
Keyerrror:'name'
in the line print row[x]. Where am I going wrong? Is it possible to fetch the column names using Dictreader? Kindly help. Thanks and regards.
在行中打印行 [x]。我哪里错了?是否可以使用 Dictreader 获取列名?请帮忙。感谢致敬。
采纳答案by Daniel
You can read the header by using the next()
function which return the next row of the reader's iterable object as a list. then you can add the content of the file to a list.
您可以使用next()
将读取器可迭代对象的下一行作为列表返回的函数来读取标题。然后您可以将文件的内容添加到列表中。
import csv
with open("C:/path/to/.filecsv", "rb") as f:
reader = csv.reader(f)
i = reader.next()
rest = [row for row in reader]
Now i has the column's names as a list.
现在我将列的名称作为列表。
print i
>>>['id', 'name', 'age', 'sex']
Also note that reader.next()
does not work in python 3. Instead use the the inbuilt next()
to get the first line of the csv immediately after reading like so:
另请注意,reader.next()
这在 python 3 中不起作用。而是使用内置next()
在读取后立即获取 csv 的第一行,如下所示:
import csv
with open("C:/path/to/.filecsv", "rb") as f:
reader = csv.reader(f)
i = next(reader)
print(i)
>>>['id', 'name', 'age', 'sex']
回答by Tania
Thanking Daniel Jimenez for his perfect solution to fetch column names alone from my csv, I extend his solution to use DictReader so we can iterate over the rows using column names as indexes. Thanks Jimenez.
感谢 Daniel Jimenez 从我的 csv 中单独获取列名的完美解决方案,我将他的解决方案扩展为使用 DictReader,以便我们可以使用列名作为索引迭代行。谢谢希门尼斯。
with open('myfile.csv') as csvfile:
rest = []
with open("myfile.csv", "rb") as f:
reader = csv.reader(f)
i = reader.next()
i=i[1:]
re=csv.DictReader(csvfile)
for row in re:
for x in i:
print row[x]
回答by user3194712
Though you already have an accepted answer, I figured I'd add this for anyone else interested in a different solution-
虽然您已经有一个可接受的答案,但我想我会为其他对不同解决方案感兴趣的人添加这个 -
- Python's DictReader object in the CSV module (as of Python 2.6 and above) has a public attribute called fieldnames. https://docs.python.org/3.4/library/csv.html#csv.csvreader.fieldnames
- CSV 模块中的 Python DictReader 对象(从 Python 2.6 及更高版本开始)有一个名为fieldnames的公共属性。 https://docs.python.org/3.4/library/csv.html#csv.csvreader.fieldnames
An implementation could be as follows:
一个实现可能如下:
import csv
with open('C:/mypath/to/csvfile.csv', 'r') as f:
d_reader = csv.DictReader(f)
#get fieldnames from DictReader object and store in list
headers = d_reader.fieldnames
for line in d_reader:
#print value in MyCol1 for each row
print(line['MyCol1'])
In the above, d_reader.fieldnamesreturns a list of your headers (assuming the headers are in the top row). Which allows...
在上面,d_reader.fieldnames返回您的标题列表(假设标题位于第一行)。这使得...
>>> print(headers)
['MyCol1', 'MyCol2', 'MyCol3']
If your headers are in, say the 2nd row (with the very top row being row 1), you could do as follows:
如果您的标题在第 2 行(最顶行是第 1 行),您可以执行以下操作:
import csv
with open('C:/mypath/to/csvfile.csv', 'r') as f:
#you can eat the first line before creating DictReader.
#if no "fieldnames" param is passed into
#DictReader object upon creation, DictReader
#will read the upper-most line as the headers
f.readline()
d_reader = csv.DictReader(f)
headers = d_reader.fieldnames
for line in d_reader:
#print value in MyCol1 for each row
print(line['MyCol1'])
回答by NYCeyes
The csv.DictReader
object exposes an attribute called fieldnames
, and that is what you'd use. Here's example code, followed by input and corresponding output:
该csv.DictReader
对象公开了一个名为 的属性fieldnames
,这就是您要使用的属性。这是示例代码,后面是输入和相应的输出:
import csv
file = "/path/to/file.csv"
with open(file, mode='r', encoding='utf-8') as f:
reader = csv.DictReader(f, delimiter=',')
for row in reader:
print([col + '=' + row[col] for col in reader.fieldnames])
Input file contents:
输入文件内容:
col0,col1,col2,col3,col4,col5,col6,col7,col8,col9
00,01,02,03,04,05,06,07,08,09
10,11,12,13,14,15,16,17,18,19
20,21,22,23,24,25,26,27,28,29
30,31,32,33,34,35,36,37,38,39
40,41,42,43,44,45,46,47,48,49
50,51,52,53,54,55,56,57,58,59
60,61,62,63,64,65,66,67,68,69
70,71,72,73,74,75,76,77,78,79
80,81,82,83,84,85,86,87,88,89
90,91,92,93,94,95,96,97,98,99
Output of print statements:
打印语句的输出:
['col0=00', 'col1=01', 'col2=02', 'col3=03', 'col4=04', 'col5=05', 'col6=06', 'col7=07', 'col8=08', 'col9=09']
['col0=10', 'col1=11', 'col2=12', 'col3=13', 'col4=14', 'col5=15', 'col6=16', 'col7=17', 'col8=18', 'col9=19']
['col0=20', 'col1=21', 'col2=22', 'col3=23', 'col4=24', 'col5=25', 'col6=26', 'col7=27', 'col8=28', 'col9=29']
['col0=30', 'col1=31', 'col2=32', 'col3=33', 'col4=34', 'col5=35', 'col6=36', 'col7=37', 'col8=38', 'col9=39']
['col0=40', 'col1=41', 'col2=42', 'col3=43', 'col4=44', 'col5=45', 'col6=46', 'col7=47', 'col8=48', 'col9=49']
['col0=50', 'col1=51', 'col2=52', 'col3=53', 'col4=54', 'col5=55', 'col6=56', 'col7=57', 'col8=58', 'col9=59']
['col0=60', 'col1=61', 'col2=62', 'col3=63', 'col4=64', 'col5=65', 'col6=66', 'col7=67', 'col8=68', 'col9=69']
['col0=70', 'col1=71', 'col2=72', 'col3=73', 'col4=74', 'col5=75', 'col6=76', 'col7=77', 'col8=78', 'col9=79']
['col0=80', 'col1=81', 'col2=82', 'col3=83', 'col4=84', 'col5=85', 'col6=86', 'col7=87', 'col8=88', 'col9=89']
['col0=90', 'col1=91', 'col2=92', 'col3=93', 'col4=94', 'col5=95', 'col6=96', 'col7=97', 'col8=98', 'col9=99']
回答by J11
I am just mentioning how to get all the column names from a csv file. I am using pandas library.
我只是提到如何从 csv 文件中获取所有列名。我正在使用熊猫库。
First we read the file.
首先我们读取文件。
import pandas as pd
file = pd.read_csv('details.csv')
Then, in order to just get all the column names as a list from input file use:-
然后,为了从输入文件中获取所有列名作为列表,请使用:-
columns = list(file.head(0))
回答by Adnan Ali
here is the code to print only the headers or columns of the csv file.
这是仅打印 csv 文件的标题或列的代码。
import csv
HEADERS = next(csv.reader(open('filepath.csv')))
print (HEADERS)
Another method with pandas
大熊猫的另一种方法
import pandas as pd
HEADERS = list(pd.read_csv('filepath.csv').head(0))
print (HEADERS)
回答by Shriganesh Kolhe
How about
怎么样
with open(csv_input_path + file, 'r') as ft:
header = ft.readline() # read only first line; returns string
header_list = header.split(',')
# returns list;
with open(csv_input_path + file, 'r') as ft:
header = ft.readline() # read only first line; returns string
header_list = header.split(',')
# 返回列表;
I am assuming your input file is CSV format. If using pandas, it takes more time if the file is big size because it loads the entire data as the dataset.
我假设您的输入文件是 CSV 格式。如果使用pandas,如果文件很大,则需要更多时间,因为它将整个数据作为数据集加载。