[英]Get 1st column values on .csv file on python
i am newbie at python programming, i have a .csv file containing mac address and signal strength data from an AP consider my csv data is: 我是python编程的新手,我有一个.csv文件,其中包含来自AP的mac地址和信号强度数据,我的csv数据为:
i want to get just mac address values which is the 1st row, referring to https://docs.python.org/2/library/csv.html i already tried 我想只获取第一行的mac地址值,请参阅https://docs.python.org/2/library/csv.html我已经尝试过了
import csv
import json
with open('DataFTrainBarucp.csv') as csvfile:
ader = csv.reader(csvfile)
for row in ader:
print row[0]
but it will print the first column from my csv, how to get 1st row data, please help 但是它将从我的csv中打印第一列,如何获取第一行数据,请帮助
Do you have to use csv
? 您必须使用
csv
吗? The pandas
package has easy to use file reading functions with a usecols option. pandas
软件包带有一个带有usecols选项的易于使用的文件读取功能。 Try something like: 尝试类似:
import pandas as pd
my_data = pd.read_csv('my_data_file', sep=',', header=0, usecols=[0])
numpy
file reading functions also have a columns option. numpy
文件读取功能还具有“列”选项。
Your code works fine for columns. 您的代码可以很好地用于列。 Maybe you meant that you want to print the row (ie the MAC address headers)?
也许您是说要打印行(即MAC地址标题)?
Here is the solution for each: 这是每个解决方案:
First Row: 第一排:
print([x for x in csv.reader(csvfile)][0])
First Column: 第一栏:
print([x[0] for x in csv.reader(csvfile)])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.