简体   繁体   English

Python csv 文件 - 打开一个 csv 文件并从 2 列中获取所有信息,获取唯一值,然后删除一些回复(没有 Pandas)

[英]Python csv file - Open a csv file and take all info from 2 columns, get unique values and then remove some replies(without Pandas)

I have created a csv file with 5 columns我创建了一个包含 5 列的 csv 文件

Machines | VM | Status | Node | Resolve

I want to take all values under Node and Resolve, find the unique values and then remove certain responses(There are some "none" and "record" there which I don't need).我想获取节点和解析下的所有值,找到唯一值,然后删除某些响应(那里有一些我不需要的“无”和“记录”)。

What is the best way to do this?做这个的最好方式是什么?

I was trying to take 1 column at a time and then putting it in sets which did work but is there a quicker way?我试图一次取 1 列,然后将其放入确实有效的集合中,但有更快的方法吗? From the set I was then trying to take away the values I didn't need but realised I was ending up with some values have \n at the end.然后我试图从集合中拿走我不需要的值,但意识到我最终得到的一些值最后有 \n 。

Usually I use Pandas which I love to us but I am unable to use this on the machine I am working on at the moment.通常我使用我喜欢的 Pandas,但我目前无法在我正在使用的机器上使用它。

unique3=[]

with open("machines.csv", "r") as file:
    mach = file.readlines()
    
     for c in mach:
        split_lines = c.split(",")[3]
        unique3.append(split_lines)
       
unique4=[]

with open("machines.csv", "r") as file2:
    mach2 = file2.readlines()

    for c in mach2:
        split_lines2 = c.split(",")[4]
        unique4.append(split_lines2)

uniqueunique = (set(unique4 + unique3))

Any help greatly appreciated, I know this is probably straight forward but I struggle with lists and strings非常感谢任何帮助,我知道这可能是直截了当的,但我在列表和字符串方面遇到了困难

Something like this:像这样的东西:

import csv

with open("machines.csv", "r") as f:
    rdr = csv.reader(f)
    next(rdr) # skip header if any, otherwise - remove this line
    *_, node, resolve = zip(*rdr)
    unique = set(node).union(set(resolve))
    print(unique)

Then you can remove unwanted values然后您可以删除不需要的值

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

相关问题 尝试从 CSV 文件中获取信息,重新排列列,然后将新输出写入 Python 中的新 CSV 文件 - Trying to take info from a CSV file, rearrange the columns and then write the new output to a new CSV file in Python 如何在没有熊猫的情况下使用python从CSV文件中获取10个最大值 - How to get the 10 highest values from a CSV file using Python without Pandas 如何在不指定列的情况下删除 Pandas 读取的任何 csv 文件中的所有多余空格? - How to remove all excessive spaces in any csv file read by Pandas without specifying columns? 从python中的csv文件中删除不需要的值 - remove unwanted values from csv file in python Python - 从csv文件中获取列数 - Python - get number of columns from csv file csv文件在没有熊猫的情况下按python中的列对行进行排序 - csv file sorting rows by columns in python without pandas 如何在python中没有pandas的情况下删除csv文件中的重复行? - How to remove duplicated rows in a csv file without pandas in python? 如何从csv文件的某些列中删除NaN值? - How can I remove NaN values from some columns in a csv file? python pandas在csv文件的特定列中删除行并替换值 - python pandas to drop lines and substitute values in specific columns of a csv file 使用python的csv文件中的列中的唯一元素 - Unique elements in columns in csv file using python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM