我有一个包含以下列的csv文件:
ID,姓名,年龄,性别
其次是上面列的很多值.我试图单独读取列名称并将它们放在列表中.
我正在使用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]
Run Code Online (Sandbox Code Playgroud)
但我想要做的是,我需要列表列表(在上面的例子中为"i")用输入csv自动解析,而不是在列表中硬编码.
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]
Run Code Online (Sandbox Code Playgroud)
这给出了一个错误
Keyerrror:'name'
Run Code Online (Sandbox Code Playgroud)
在行打印行[x].我哪里错了?是否可以使用Dictreader获取列名?请帮助.感谢致敬.
use*_*712 66
虽然你已经有了一个公认的答案,但我想我会为其他对不同解决方案感兴趣的人添加这个答案 -
实施可以如下:
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'])
Run Code Online (Sandbox Code Playgroud)
在上面,d_reader.fieldnames返回标题列表(假设标题位于顶行).这使得...
>>> print(headers)
['MyCol1', 'MyCol2', 'MyCol3']
Run Code Online (Sandbox Code Playgroud)
如果您的标题位于第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'])
Run Code Online (Sandbox Code Playgroud)
Dan*_*nez 42
您可以使用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]
Run Code Online (Sandbox Code Playgroud)
现在我将列的名称作为列表.
print i
>>>['id', 'name', 'age', 'sex']
Run Code Online (Sandbox Code Playgroud)
另请注意,reader.next()在python 3中不起作用.而是使用next()inbuilt在读取之后立即获取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']
Run Code Online (Sandbox Code Playgroud)
NYC*_*yes 13
该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])
Run Code Online (Sandbox Code Playgroud)
输入文件内容:
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
Run Code Online (Sandbox Code Playgroud)
输出印刷报表:
['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']
Run Code Online (Sandbox Code Playgroud)
怎么样
with open(csv_input_path + file, 'r') as ft:
header = ft.readline() # read only first line; returns string
header_list = header.split(',') # returns list
Run Code Online (Sandbox Code Playgroud)
我假设您的输入文件是 CSV 格式。如果使用 pandas,如果文件很大,则需要更多时间,因为它将整个数据加载为数据集。
| 归档时间: |
|
| 查看次数: |
84517 次 |
| 最近记录: |