简体   繁体   English

从文件获取和读取数据时出错

[英]Errors with obtaining and reading data from a file

I'm trying to create a program that reads information about cinema times from a .csv file, where the user can search for movie times by the month it is shown, the company producing the movie and the name of the movie. 我正在尝试创建一个程序,该程序从.csv文件读取有关电影时间的信息,用户可以在其中按显示的月份搜索电影时间,制作电影的公司和电影名称。

The file is formatted like: 该文件的格式如下:

Movie name, date , cinema, post code of cinema, company producing, cost

for each line. 每行。

The only information that I need to search for are the dates, company producing and the name of the movie. 我需要搜索的唯一信息是日期,公司制作和电影名称。

The first problem I have with this code is, when I search for movies by a company, only the first movie by the company in the file is shown and I want to show all of them. 这段代码的第一个问题是,当我搜索某公司的电影时,仅显示文件中该公司的第一部电影,而我想全部显示。

The second problem I have is that I need to search for movies by month, but the format for date in the file is dd/mm/yy and I am not sure how to search for just the month and ignore the day and year. 我遇到的第二个问题是我需要按月份搜索电影,但是文件中日期的格式为dd/mm/yy ,我不确定如何仅搜索月份而忽略日期和年份。

If anyone has a solution to either of these problems, I would very much appreciate it and thanks in advance. 如果有人对这些问题中的任何一个都有解决方案,我将不胜感激,并在此先感谢。

For your first problem it seems that your code is correct, I've tested it out and it does indeed print a list where there is a match for company: 对于您的第一个问题,您的代码似乎是正确的,我已经对其进行了测试,并且确实打印了与公司匹配的列表:

>>> company = "one"
>>> moviedata = [("one","two"), ("two","one"), ("two","three")]
>>> print([s for s in moviedata if (company) in s])
[('one', 'two'), ('two', 'one')]

I can only suggest to print out and check if your moviedata is stored in list of tuples. 我只能建议打印并检查您的moviedata是否存储在元组列表中。

for second problem just slice the data you have to retrieve the month: 对于第二个问题,只需对要检索月份的数据进行切片:

>>> "03/04/2014"[3:5]
'04'

example for your code: 您的代码示例:

>>> moviedata = [("one","two"), ("two","one"), ("two","three")]
>>> moviedata[1]
('two', 'one')
>>> moviedata[1][1]
'one'
>>> moviedata[1][1][:2]
'on'

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

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