简体   繁体   English

如何通过用户输入从 CSV 文件中打印某些数据

[英]How do I print certain data from a CSV file through user input

I created a CSV file that stores a book, its author and the year the book released, through user input.我创建了一个 CSV 文件,该文件通过用户输入存储一本书、作者和出版年份。 I want to now ask the user to choose an author and the program should display all the books by that author, but I'm not sure how to do this我现在想要求用户选择一个作者,程序应该显示该作者的所有书籍,但我不知道该怎么做

Here is my code so far:到目前为止,这是我的代码:

import csv

amount = int(input("How many records would you like to store: "))
count = 0

with open("Books.csv", "w", newline="") as file:
    writer = csv.writer(file)
    writer.writerow(["", "Book", "Author", "Year released"])

while count < amount:
    count += 1

    book = input("Enter a book: ")
    author = input("Enter it's Author: ")
    year = input("Enter the year it released: ")

    headers = [count, book, author, year]
    with open("Books.csv", 'a') as f:
        writer = csv.writer(f)
        writer.writerow(headers)

This should do.这应该做。

Example contents of Books.csv Books.csv 的示例内容

,Book,Author,Year released
0,1984,G. Orwell,1949
1,Brave New World,A. Huxley,1932
2,Johnny Got His Gun,D. Trumbo,1939

The code编码

import csv

# getting author and book column indexes in the csv
COLUMNS = ["", "Book", "Author", "Year released"]    # first column is the book no.
AUTHOR_COLUMN_INDEX = COLUMNS.index("Author")
BOOK_COLUMN_INDEX = COLUMNS.index("Book")

author = input("Enter the name of an author: ")

with open('Books.csv', 'r', newline='') as f:
    for line in csv.reader(f):
        if len(line) == len(COLUMNS):   # if empty line or invalid line
            if line[AUTHOR_COLUMN_INDEX] == author:
                print(line[BOOK_COLUMN_INDEX])

When the code is run:代码运行时:

Enter the name of an author: A. Huxley
Brave New World

暂无
暂无

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

相关问题 如何读取 CSV 文件两次并根据通过函数传递的参数打印某些行? - How do I read through a CSV file twice and print certain rows based on arguments passed through the function? 如何在csv文件中的行中打印特定字段,以及如何将输入内容写入csv文件? - How do I print specific fields from rows in a csv file and how to write input to a csv file? 如何根据用户输入显示CSV数据? - How do I display data from CSV based on user input? 如何从使用熊猫上传的大型CSV文件中仅打印出某些行 - How do I print out only certain rows from a large CSV file uploaded using pandas 如何使用csv文件检查用户输入并从特定列打印数据? - How to check user input with csv file and print data from specific column? CSV 文件 - 如何通过用户输入替换一行数据? - CSV files - How do I replace a row of data through user input? 在不使用熊猫的情况下,如何分析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 do I print data in a csv file that corresponds to another column? 如何计算csv文件中的特定数据并在python中打印该数字? - How do I count specific data from a csv file and print that number in python? 如何从CSV文件中排除某个组? - How do i exclude a certain group from a csv file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM