简体   繁体   English

如何在打印一定范围内的值时读取CSV文件中的特定行和列

[英]How do I read specific rows AND columns in CSV file while printing values within a certain range

Hi I am having some problems looking at specific rows and columns in an csv file. 嗨,我在查看csv文件中的特定行和列时遇到一些问题。

My current goal is look 3 different columns out the several that are there. 我当前的目标是在其中的三个不同的列中查找。 And on top of that, I want to look at the data values (ex. 0.26) and sort through the ones that are betweeen 0.21 and 0.31 in a specific column. 最重要的是,我想查看数据值(例如0.26),并在特定列中对0.21和0.31之间的数据进行排序。 My issue is that I dont know how to do both of those at the same time. 我的问题是我不知道如何同时做这两个。 I keep getting errors that tell me i cant use '<=' with float and str. 我不断收到错误消息,告诉我我不能在float和str中使用'<='。

Heres my code: 这是我的代码:

    import csv
    from collections import defaultdict

    columns = defaultdict(list) # each value in each column is appended to a list

    with open('C:\\Users\\AdamStoer\\Documents\\practicedata.csv') as f:
    reader = csv.DictReader(f,delimiter=',') # read rows into a dictionary format
    for row in reader:
        for columns['pitch'] in row:
            for v in columns['pitch']:
                p=float(v)
                if p <= 0.5:
                    columns['pitch'].append(v)
    print(columns['pitch'])

This code was working before for the last part 该代码在最后一部分之前是有效的

for row in reader: # read a row as {column1: value1, column2: value2,...}
    for (k,v) in row.items(): # go over each column name and value 
        columns[k].append(v) # append the value into the appropriate list
                             # based on column name k
print(columns['pitch'])

Looks like you're confusing a couple things. 看起来您在混淆两件事。 If you know the specific column you want (pitch) you do not have to loop over all the columns in each row. 如果知道想要的特定列(间距),则不必遍历每一行中的所有列。 You can access it directly like so: 您可以像这样直接访问它:

for row in reader:
    p = float(row['pitch'])
    if p <= 0.5:
        print p

It's hard for me to tell what output you want, but here's an example that looks at just the pitch in each row and if it is a match appends all the target values for that row to the columns dictionary. 我很难说出您想要什么输出,但是这里有一个示例,仅查看每行的音高,如果匹配,则将该行的所有目标值附加到column字典中。

targets = ('pitch', 'roll', 'yaw')
columns = defaultdict(list)

for row in reader:
    p = float(row['pitch'])
    if p >= 0.21 and p <= 0.31:
        for target in targets:
            column[target].append(row[target])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在不使用熊猫的情况下,如何分析CSV数据并仅从CSV文件的某些列和行中提取某些值? - WITHOUT using Pandas, how do I analyze CSV data and only extract certain values from certain columns and rows of my CSV file? 如何在具有列表列值列表且某些行具有双引号作为字符串的csv文件中读取 - How to read in a csv file which has list of list columns values with certain rows having double quotes as strings 如何读取 CSV 文件两次并根据通过函数传递的参数打印某些行? - How do I read through a CSV file twice and print certain rows based on arguments passed through the function? 当数据始终处于特定顺序但值不同时,如何在CSV文件中查找特定值 - How do I find specific values inside of a CSV file when the data is always in a certain order but with different values 如何根据该文件中行中的日期将 CSV 文件在特定日期之间的值传输到另一个 CSV 文件? - How do I transfer values of a CSV files between certain dates to another CSV file based on the dates in the rows in that file? 如何读取文件中的特定列并将它们变成新的 csv - How do I read specific columns in a file and make them into a new csv Python:使用Excel CSV文件只读取某些列和行 - Python: Using Excel CSV file to read only certain columns and rows 如何在不使用熊猫的情况下从csv文件获取特定范围内的特定列 - How to get specific columns in a certain range from a csv file without using pandas 如何识别指定列范围内的某些行? - How to identify certain rows within a specified range of columns? 如何使用特定列比较另一个CSV中的行,从而删除一个CSV中的行 - How do I delete rows in one CSV based on rows in another CSV using specific columns to compare
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM