简体   繁体   English

如何获取 str/.txt 输入并将其放入 pyton 列表中

[英]How can I take a str/.txt input and make it into a list for pyton

I have a text file where the information that is formated as我有一个文本文件,其中的信息格式为

Title, Author, User Rating, Reviews, Price, Publication Year, Genre(Fiction or nonfiction)标题、作者、用户评分、评论、价格、出版年份、流派(小说或非小说)

sample data form txt样本数据表格txt

test_data = """ 
    Girls,Hopscotch Girls,4.8,9737,7,2019,Non Fiction
    I - Alex Cross,James Patterson,4.6,1320,7,2009,Fiction
    If Animals Kissed Good Night,Ann Whitford Paul,4.8,16643,4,2019,Fiction
 """

They are all separated by a comma (,).它们都用逗号 (,) 分隔。 I wanted to take this input and make it into a list that looks something like我想把这个输入变成一个看起来像的列表

list = {'Name': 'Girls','Author': 'Hopscotch Girls','User Rating':'4.8', 'Reviews':'9737', 'Price':'7', 'Publication Year':'2019', 'Genre':'Non Fiction'}

I'm trying to make it a list so its easier to look through because later on in my program since I want to be able to get user input like year and list all the books within the year with.我试图让它成为一个列表,以便更容易浏览,因为稍后在我的程序中,因为我希望能够获得像年份这样的用户输入并列出该年内的所有书籍。 It will also make formating an output easier.它还将使格式化输出更容易。

If you are reading the data from a .txt file如果您从 .txt 文件中读取数据

data = {'Name': [], 'Author': [], 'User Rating': [], 'Reviews': [], 'Price': [], 'Publication Year': [], 'Genre': []}

with open("filename/goes/here.txt", "r") as f:
    for line in f.readlines():
        lineData = line.split(r",")

        for key, ld in zip(data.keys(), lineData):
            data[key].append(ld)

From your description I believe this is a solution.根据您的描述,我相信这是一个解决方案。 Some advice, never name your variables with the same name as any built-in functions.一些建议,永远不要使用与任何内置函数相同的名称来命名变量。 (eg list, int, str) (例如列表、整数、字符串)

Given your requirement鉴于您的要求

I'm trying to make it a list so its easier to look through because later on in my program since I want to be able to get user input like year and list all the books within the year with.我试图让它成为一个列表,以便更容易浏览,因为稍后在我的程序中,因为我希望能够获得像年份这样的用户输入并列出该年内的所有书籍。 It will also make formating an output easier.它还将使格式化输出更容易。

I think that a list of dictionaries would do the job.我认为字典列表可以完成这项工作。 Using list comprehension (I am using the test_data you defined in the question):使用列表理解(我使用的您在问题中定义的test_data ):

tags = ["Name", "Author", "User Rating", "Reviews", "Price", "Publication Year", "Genre"]
result = [dict(zip(tags, x.split(","))) for x in test_data.split("\n") if len(x.strip()) > 0]

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

相关问题 如何让itemgetter从列表变量中获取输入? - How can I make itemgetter to take input from list variable? “TypeError:当使用 txt 文件作为输入时,只能将 str(不是“list”)连接到 str” - "TypeError: can only concatenate str (not "list") to str" when using a txt file as input 我该如何使print()函数不接收输入字段中的文本 - How can I make the print() function not take the text in the input field with it 如何使该脚本接受循环输入? - How can I make this script to take loop input? 如何使张量流模型将列表作为输入? - How can I make a tensorflow model take lists as input? 如何让海龟在以下条件下接受用户输入? - How can I make the turtle take user input in the following condition? 如何从文件中获取列表输入并将其转换为字典? - How can I take a list input from a file and turn it into a dictionary? 如何在导入 numpy 模块时获取用户输入“n = str(input("Give str"))”? - How do I take user input "n = str(input("Give str"))" while having the numpy module imported? 如何创建一个 if 语句来检查输入是否为 str/int - How can you make an if statement that checks if input is a str/int 如何从tkinter获取条目并将其打印在txt文档中? - How can I take an entry from tkinter and print it in a txt document?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM