简体   繁体   English

如何只打印包含某个字符串的文件中的行?

[英]How to only print lines from a file containing a certain string?

I have some code here that takes data from a file and asks the user what location they want to see data for.我这里有一些代码从文件中获取数据并询问用户他们想要查看数据的位置。

def main():
    #ask user for a city or county and store that as a variable
    location = input("Please enter a location. Make sure to add County if the location is a county ")
    location = location.capitalize()

    print("=== Data for",location," ===")
    print("Date  Total Cases  Hospitalizations  Deaths")

    #open the file
    covidFile = open("Covid Data.txt", "r")

    #read the first line to move it aside from the rest of the data
    firstLine = covidFile.readline()

    #read the rest of the data in the file using a for loop
    for dataLine in covidFile:

        #strip the data line
        dataLine = dataLine.rstrip("\n")

        #split the line into a dataList
        dataList = dataLine.split(",")

        #if location matches dataList[2]
        if location in dataList:
            date = str(dataList[0])
            cases = int(dataList[4])
            hospitalizations = int(dataList[5])
            deaths = int(dataList[6])
            print(date,"  ",cases,"  ",hospitalizations,"  ",deaths)

        #if location does not match dataList[2]
        else:
            print("No data was found for that city or county.")

    #close file
    covidFile.close()

However, there's a problem where the code will print out long strings of "No data was found for that city or county", followed by a line of code from the file.但是,代码会打印出长字符串“没有找到该城市或县的数据”,然后是文件中的一行代码。 I'm trying to figure out how I fix the code so that it only prints out data for the location that the user types in (if the location is in the file, of course).我试图弄清楚如何修复代码,以便它只打印出用户键入的位置的数据(当然,如果该位置在文件中)。

I an bad at english so I can mis understood your question我英语不好,所以我会误解你的问题

lines = file.open(*.txt)
lines = lines.readlines()
list = []
for x in lines:
    if str in x:
        list.append(x)

Have you tried entirely removing the else statement?您是否尝试过完全删除 else 语句? I mean you only need to print out whenever the location is found.我的意思是你只需要在找到位置时打印出来。

If you still need to print something when nothing is found you could create a boolean, lets say data_found = False and mark it as True if you ever enter the if statement.如果您在没有找到任何内容时仍需要打印某些内容,您可以创建一个 boolean,假设data_found = False并将其标记为 True 如果您曾经输入 if 语句。 Then at the very end of the script after closing the file you check if not data_found and there you print your "No data was found for that city or county."然后在关闭文件后脚本的最后,您检查是否not data_found并在那里打印您的“未找到该城市或县的数据”。

Just use a variable to store if the data is found:如果找到数据,只需使用变量存储:

found = False
for dataline in Covidfile:
    if condition:
        .
        .
        .
        found = True
if not found:
    print("No data was found for that city or county.")

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

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