[英]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.