![](/img/trans.png)
[英]How to read from a csv file and add the values as headers of pandas dataframe in python?
[英]How to add Headers to read output from Python CSV File?
我有一個 csv 文件,看起來像這樣:
我有以下代碼,它讀取 csv 文件,然后可以打印/訪問 CSV 文件中的信息。
import csv
class CsvReader:
with open("Items.csv") as fp:
reader = csv.reader(fp, delimiter=",", quotechar='"')
next(reader, None) # skip the headers
data_read = [row for row in reader]
print(data_read[0])
我從印刷品中得到了 output
['1', '5.99$, '1', 'Blueberry Muffin']
我將如何將其格式化為字典,將標題作為鍵,將信息作為元素?
例如,代碼為 output:
{Item #: 1, Price: 5.99, Quantity: 1, Name: Blueberry Muffin}
我在這篇文章中引用並看到了很多相似之處: How do I read and write CSV files with Python?
但找不到更多關於如何以這種方式專門格式化 output 的細節,而不使用諸如 pandas 之類的東西,我不想使用它。
如果您希望將字典鍵作為字段即列,那么您為什么要跳過它們。 這里是簡單的解決方案。
import csv
class CsvReader:
with open("Item.csv") as fp:
reader = csv.reader(fp, delimiter=",", quotechar='"')
fields = next(reader)
data_read = []
for row in reader:
data_read.append(dict(zip(fields, row)))
print(data_read[0])
首先將列名和 map 與每個行元素一起存儲。
有幾種方法可以做到這一點......而且我同意使用 Pandas 對於讀取簡單文件可能是過大的。 您可能會爭辯說,即使使用csv_reader
也是矯枉過正。 :)
無論如何,這里有 3 種變體。 您需要做的就是捕獲標簽並將它們用作字典中的鍵。 意識到下面的方法將為您提供“字典列表”(或 pandas-speak 中的“記錄”類型格式)。 另一種選擇是使用項目編號作為第一個鍵的“字典字典”,但本質上這與列表索引相同......所以大致相同。 您也可以放棄捕獲項目編號,因為這只是結果列表中的索引,但這是細微差別。
您可能還對在最后一個變體中顯示的named tuple
中捕獲它們感興趣。 非常容易使用...
# Grocery Reader
import csv
from collections import namedtuple
with open("data.csv") as fp:
reader = csv.reader(fp, delimiter=",", quotechar='"')
labels = next(reader, None) # capture the headers
result = []
for row in reader: # iterate the remaining rows
pairs = zip(labels, row)
result.append(dict(pairs))
print(result)
# the above isn't real satisfying as the numeric objects are captured as strings.
# so...
with open("data.csv") as fp:
reader = csv.reader(fp, delimiter=",", quotechar='"')
labels = next(reader, None) # capture the headers
result = []
for row in reader: # iterate the remaining rows
row[0] = int(row[0])
row[1] = float(row[1])
row[2] = int(row[2])
pairs = zip(labels, row)
result.append(dict(pairs))
print(result)
with open("data.csv") as fp:
reader = csv.reader(fp, delimiter=",", quotechar='"')
labels = next(reader, None) # capture the headers
# make lowercase...just for standardization
labels = [t.lower() for t in labels]
Grocery = namedtuple('Grocery', labels)
result = []
for row in reader: # iterate the remaining rows
row[0] = int(row[0])
row[1] = float(row[1])
row[2] = int(row[2])
grocery = Grocery(*row)
result.append(grocery)
for grocery in result:
# the below presumes you know the names inside the named tuple...
print(f'a {grocery.name} costs {grocery.price}')
[{'Item': '1', 'Price': '4.99', 'Qty': '2', 'Name': 'Muffin'}, {'Item': '2', 'Price': '1.25', 'Qty': '6', 'Name': 'Gum'}, {'Item': '3', 'Price': '2.50', 'Qty': '8', 'Name': 'Cookie'}]
[{'Item': 1, 'Price': 4.99, 'Qty': 2, 'Name': 'Muffin'}, {'Item': 2, 'Price': 1.25, 'Qty': 6, 'Name': 'Gum'}, {'Item': 3, 'Price': 2.5, 'Qty': 8, 'Name': 'Cookie'}]
a Muffin costs 4.99
a Gum costs 1.25
a Cookie costs 2.5
從這里使用DictReader
csv 。
cat food.csv
Item #,Price,Quantity,Name
1, 5.99$,1,Blueberry Muffin
import csv
with open('food.csv') as csv_file:
reader = csv.DictReader(csv_file,delimiter=",", quotechar='"')
for row in reader:
print(dict(row))
{'Item #': '1', 'Price': ' 5.99$', 'Quantity': '1', 'Name': 'Blueberry Muffin'}
使用csv.DictReader
並跳過第一行:
import csv
with open("tmp/1.csv", "r") as f:
reader = csv.DictReader(
f, fieldnames=["Item", "Price", "Quantity", "Name"]
)
# skipping existing header
it = iter(reader)
next(it, None)
for row in it:
print(row)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.