简体   繁体   English

Python 在 csv 列中搜索字符串并在另一个 csv 文件中写入行

[英]Python search for string in csv columns and write row in another csv file

I'm fairly new to coding but I was trying to write a code that reads over a csv file to check if strings that the user inputs are in specific columns and then writes out the row in which the strings are present in a separate csv file.我对编码还很陌生,但我试图编写一个读取 csv 文件的代码,以检查用户输入的字符串是否在特定列中,然后在单独的 csv 文件中写出字符串所在的行. The issue is that when I run the code, it only prints out 1 row rather than multiple rows that have the strings within them.问题是当我运行代码时,它只打印出 1 行而不是其中包含字符串的多行。 I also tried to make it case insensitive but somehow it doesn't work either.我也试图让它不区分大小写,但不知何故它也不起作用。

My code right now looks like this:我的代码现在看起来像这样:

import csv
shape = input("Type in a shape ")
color = input("Type in a color")
with open("directory.csv", "r") as f:
    csvreader = csv.reader(f, delimiter=",")
    for row in csvreader:
      if shape.lower() in row[7] and color.lower() in row[6] :
         with open("data.csv", "w") as file:
            writer = csv.writer(file, lineterminator = "\n")
            writer.writerow(row)

Any help or suggestions are very much needed!非常需要任何帮助或建议!

pandas is here to help you. pandas 在这里为您提供帮助。 use the fragment below.使用下面的片段。

import pandas as pd
df = pd.read_csv("directory.csv")
df1 = df[df["column1"] == shape & df["column2"] == color]
df1.to_csv("found_data.csv")

data.csv is truncated every time it's opened in 'w' mode, so it will only contain the final match. data.csv每次以'w'模式打开时都会被截断,因此它只会包含最终匹配。 Moving the open statement to the beginning of the code fixes the problem.open语句移到代码的开头可以解决问题。

import csv
shape = input("Type in a shape ")
color = input("Type in a color")
with open("directory.csv", "r") as f, open("data.csv", "w") as file:
    csvreader = csv.reader(f, delimiter=",")
    for row in csvreader:
        if shape.lower() in row[7] and color.lower() in row[6] :
            writer = csv.writer(file, lineterminator = "\n")
            writer.writerow(row)

Alternatively, the file could be opened in 'a' (append) mode, and then existing lines in the file will be preserved:或者,可以在'a' (附加)模式下打开文件,然后将保留文件中的现有行:

import csv
shape = input("Type in a shape ")
color = input("Type in a color")
with open("directory.csv", "r") as f:
    csvreader = csv.reader(f, delimiter=",")
    for row in csvreader:
      if shape.lower() in row[7] and color.lower() in row[6] :
         with open("data.csv", "a") as file:
            writer = csv.writer(file, lineterminator = "\n")
            writer.writerow(row)

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

相关问题 Python读取CSV文件,并写入其他跳过列 - Python read CSV file, and write to another skipping columns 比较两个csv文件中的列并将其写入python中的另一个文件 - Compare columns in two csv files and write it to another file in python Python 程序:循环输入 csv 文件中的字符串; 在另一个 csv 文件中搜索匹配的字符串; 将匹配的行内容返回给另一个 csv - Python program: loop around strings from input csv file; search the matching string in another csv file; return matching row content to another csv 在 csv 上写入但得到没有行和列的空文件 - write on csv but getting empty file with no row and columns 使用 Python 在 csv 文件中从字符串搜索中打印多列 - Print multiple columns from string search in csv file using Python Python3:在csv文件中搜索字符串并返回行和列 - Python3: search csv file for string and return row and column Python CSV读取文件并选择列并写入新的CSV文件 - Python CSV read file and select columns and write to new CSV file 如何在 CSV 文件的列中搜索字符串 - How to search for a string in columns of a CSV file 在python中的2列CSV文件中写入2个列表 - Write 2 lists in 2 columns of CSV file in python 将 csv 中的某些列附加到另一个 csv 文件不会拆分每一行 - appending certain columns in csv to another csv file is not spilting each row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM