简体   繁体   English

在Python上的.csv文件上获取第一列值

[英]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数据为:

我的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.

相关问题 提取 HTML 表数据从 email 到 csv 文件,第一列值到行标题,使用 ZA7F5F35426B928727111 - Extracting HTML table data from email to csv file, 1st column values to row headers, using Python 当我将第一列作为输入传递给函数时,从 csv 文件中获取第二列记录 - Get the 2nd column records from the csv file when i pass 1st column as a input to the function 连接Python中具有相同第一列值的CSV文件的所有行 - Joining all rows of a CSV file that have the same 1st column value in Python Python:根据第一列中的值提取excel单元格值 - Python: Extracting excel cell values based on value in 1st column 如何在python数据框中的列中获取值的第一次出现 - How to get the 1st occurence of a value in a column in python dataframe python比较2列,如果第3列与第2列匹配,则使用第1列的值写第4列 - python compare 2 columns and write a 4th column with values from 1st column if column 3 matches column 2 将第一列值与 Pandas 中的列标题连接起来,以获得一个又高又瘦的格式表 - Concatenate 1st Column values with column headers in Pandas to get a tall and skinny format tabel 合并多个文件:第一列(相同的字符串),第二列(每个文件的唯一值) - Merge multiple files: 1st Column (same string), 2nd Column (unique values per file) 使用 python 使用 csv 文件的第二行信息更新第一行标题 - Update 1st row headers with info from the 2nd row for csv file using python 如果第一列值相同,MatLab(或任何其他语言)转换矩阵或csv以将第二列值放到同一行? - MatLab (or any other language) to convert a matrix or a csv to put 2nd column values to the same row if 1st column value is the same?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM