简体   繁体   English

将过滤器 csv 数据导出到新的 csv (python)

[英]Export filters csv data to a new csv (python)

I would like to be able to read a csv file, filter out the data that I need based on the input of a user and then take that output and write it to a new CSV file.我希望能够读取 csv 文件,根据用户的输入过滤掉我需要的数据,然后将 output 写入新的 ZCC8D68C551C4A9A6D5313E07DE4DEAFD 文件。

A manual example would be to take a table in excel, filter specific columns to a certain value and take that filtered output and move it to a new file so you only have the filtered data.一个手动示例是在 excel 中获取一个表,将特定列过滤到某个值,然后将过滤后的 output 移到一个新文件中,这样您就只有过滤后的数据了。

I have a csv file that I am reading in我有一个正在阅读的 csv 文件

    reader = csv.reader(data, delimiter= ',')```

I am then asking a user for input

```object1 = input('What is the first thing you need?)```
```object2 = input('What what is the second thing you need?')```

```def objs():
            values = ''
            for row in reader:
                if row[3] == object1 and row[14] == object2: 
                    values = row[0], row[1], row[3], row[7], row[14]
                    print(values)
            return values```


When I print the output of def objs I get exactly what I am looking for. However it is all one line and not separated by columns. 

How can I take this output and move it to a new .csv file with headers and so each indexed row has its own column?
  
 

I believe part of the problem is that the values variable doesnt hold on to anything, as in it gets overwritten after every loop.我认为问题的一部分是 values 变量没有保留任何东西,因为它在每次循环后都会被覆盖。 Your print statement works because it is inside the loop but the return statement will only return the values for the last loop.您的 print 语句有效,因为它在循环内,但 return 语句只会返回最后一个循环的值。

def objs():
    values = '' 
    for row in reader:
        if row[3] == object1 and row[14] == object2: 
            values = [row[0], row[1], row[3], row[7], row[14]] #directly overwrites values variable
            print(values) 
    return values #this would return the last 'values' variable processed, not all of them 

Since you wanted to export them to a csv file, instead make a list that will hold lists of the values:由于您想将它们导出到 csv 文件,因此请创建一个包含值列表的列表:

def objs():
    values = []
    for row in reader:
        if row[3] == object1 and row[14] == object2:
            # this will take your wanted values and add them to a list, which then gets added to values
            values.append([row[0], row[1], row[3], row[7], row[14]])
    return values

Now you can export them to csv.现在您可以将它们导出到 csv。 Full code:完整代码:

reader = csv.reader(data, delimiter= ',')

object1 = input('What is the first thing you need?')
object2 = input('What what is the second thing you need?')

def objs():
    values = []
    for row in reader:
        if row[3] == object1 and row[14] == object2:
            # this will take your wanted values and add them to a list, which then gets added to values
            values.append([row[0], row[1], row[3], row[7], row[14]])
    return values

with open("out.csv", "w", newline="") as f:
    writer = csv.writer(f)
    values = objs()
    headers = ["row0", "row1", "row3", "row7", "row14"]

    writer.writerow(headers)
    writer.writerows(values)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM