【问题标题】:While loop isn't outputting the correct data虽然循环没有输出正确的数据
【发布时间】:2017-12-06 01:40:56
【问题描述】:

所以我按照大家的建议对我的代码进行了更正。我能够进入循环,但是一旦我输入了正确的名称,我仍然无法摆脱它。有什么建议? 这是我得到的:

import csv

full_name = input('Enter your full name: ').lower()

with open('Report1.csv') as csvfile:
    hour_summation = {}
    read_csv = csv.reader(csvfile, delimiter=',')
    for row in read_csv:
        while (' '.join((row[0], row[1]))).lower() != full_name.strip().lower():
            print('Name is not in system')
            full_name = input('Enter your full name: ').lower()
        if(' '.join((row[0], row[1]))).lower() == full_name.strip().lower():
            hour_summation[row[2]] = hour_summation.get(row[2], 0) + int(float(row[3]))
print('This is {} full hours report:'.format(full_name))
for k, v in hour_summation.items():
    print(k + ': ' + str(v) + ' hours')

这是我输入时的结果: 供参考。 Steve Miller 不在 csv 文件中,因此第一个响应是正确的。但是,Sri Mantri 在文件中,它应该继续打印出她名下的所有列表。

Enter your full name: Steve Miller
Name is not in system
Enter your full name: Sri Mantri
Name is not in system

这是代码运行时的输出结果。

Enter your full name: Sri mantri
This is sri mantri full hours report:
Beeline Blank: 28 hours
SRV-0001 Service Requests for Base and Direct Services: 4 hours
SUP-0001 Support Requests with a CISM Ticket: 129 hours
SUP-2503 Web Application Maintenance & Support: 72 hours
0026184229 Margin Controlling Java Rewrite: 4 hours
0033472751 PRE-AFE 2017 - CMS Enhancements: 2 hours
0033472863 PRE-AFE 2017 - BPM Enhancements: 67 hours
APP-10008 Pre-Series (Non-Mainframe): 4 hours
APP-10146 Logistics (Non-Mainframe): 3 hours
APP-10195 Vehicle Labor System (Mainframe): 3 hours
APP-10354 Web PartsPro (Non-Mainframe): 1 hours
APP-10431 VIPService (Non-Mainframe): 1 hours
APP-10432 VIPService (Mainframe): 3 hours
APP-10536 Truck Invoice Adjustments (Mainframe): 2 hours

csv 看起来像这样:

   First Name   Last Name   Activity    Hours
Sri Mantri  SUP-2503 Web Application Maintenance & Support  11
Sri Mantri  SUP-2503 Web Application Maintenance & Support  3
Sri Mantri  SUP-2503 Web Application Maintenance & Support  5
Sri Mantri  SUP-2503 Web Application Maintenance & Support  2
Jeff    Moore   SUP-2503 Web Application Maintenance & Support  3
David   Ayers   SUP-2507  NAFTA MFTS OS Support 10
Prasanth    Musunuru    0020826809 Vertex 6.0 at the NDC    4
Prasanth    Musunuru    0020826809 Vertex 6.0 at the NDC    3
Prasanth    Musunuru    0020826809 Vertex 6.0 at the NDC    1
Prasanth    Musunuru    0020826809 Vertex 6.0 at the NDC    1
Jeff    Moore   0024480049 Fuel Tanks (infrastructure) - time tracking  1
Jeff    Moore   0024480049 Fuel Tanks (infrastructure) - time tracking  1
Jeff    Moore   0024480049 Fuel Tanks (infrastructure) - time tracking  4

【问题讨论】:

  • 欢迎来到 StackOverflow。请阅读并遵循帮助文档中的发布指南。 Minimal, complete, verifiable example 适用于此。在您发布 MCVE 代码并准确描述问题之前,我们无法有效地帮助您。我们应该能够将您发布的代码粘贴到文本文件中并重现您描述的问题。在这种情况下,请提供数据文件并硬编码一个名称作为搜索目标。
  • 使用print() 显示变量中的值,你会看到它们是否不同。
  • 你为什么用name来获得第二个input()而你检查full_name?你应该只使用full_name
  • 永远不要改变问题 - 现在我们的答案不适合您的问题。如果您有新问题,请使用按钮 Add Question 创建新问题
  • 我投票结束这个问题作为离题,因为 OP 更改了文本,现在它是不同的问题。它不适合现有的答案。

标签: python loops csv while-loop


【解决方案1】:

如果您真的对其他解决方案感兴趣,请选择我的答案,否则请跳过它。使用 pandas 可以更轻松地解决您的需求。

所以我有一个包含这些值的 CSV 文件(“names.csv”),

Name    Hours
Sri Mati    1
Some Name   2

代码如下:

import pandas

if __name__ == "__main__":
    csv = pandas.read_csv("names.csv")
    name = input("Enter your name: ")
    if (csv["Name"]==name).any():
        print("Name Present")
        correct_name = csv.loc[csv["Name"] == name]
        # print everything
        print(csv)
        print()
        #print correct name
        print(correct_name)
        print() # for clear display
        # get individual things
        print("Correct Inidividual Values")
        print(correct_name.values)
    else:
        print("Name not there")

示例输入和输出:

Enter your name: Steve Miller
Name not there

下一次运行,

Enter your name: Sri Mati
Name Present
        Name  Hours
0   Sri Mati      1
1  Some Name      2

       Name  Hours
0  Sri Mati      1

Correct Inidividual Values
[['Sri Mati' 1]]

【讨论】:

    【解决方案2】:

    您的 while 循环中缺少将 name 连接到 full_name 的一行:

    full_name = name.lower()
    

    将此添加到您的 while 循环中,就在 input() 调用下方,就像您在文件顶部所做的那样。

    在您的 while 循环中,它应该说:

    name = input('Enter your full name: ')
    full_name = name.lower()
    

    这至少会让你的程序运行得更远!

    另外,请注意您的程序的逻辑可能有缺陷...您逐步检查 CSV 的每一行,检查一个人的姓名。这意味着如果您在 CSV 文件中有多个条目(即 - CSV 文件包含的不仅仅是一个人的信息),您只能真正访问列表中的第一个条目。您可能应该提示输入用户名,然后遍历 CSV 的每一行以检查匹配项。仅当整个 CSV 中没有匹配项时,您才应该要求另一个名称...只要看看...

    【讨论】:

      【解决方案3】:

      在此代码中,您使用 name 获取名称,但后来您使用 full_name

       while (' '.join((row[0], row[1]))).lower() != full_name.strip():
              print('Name is not in system')
              name = input('Enter your full name: ')
      

      你应该只使用full_name(它需要lower()

       while (' '.join((row[0], row[1]))).lower() != full_name.strip():
              print('Name is not in system')
              full_name = input('Enter your full name: ').lower() # <-- full_name
      

      或将name 转换为full_name

       while (' '.join((row[0], row[1]))).lower() != full_name.strip():
              print('Name is not in system')
              name = input('Enter your full name: ')
              full_name = name.lower() # <-- full_name
      

      【讨论】:

      • 看看我在下面所做的更改。
      猜你喜欢
      • 2023-03-28
      • 1970-01-01
      • 1970-01-01
      • 2011-01-19
      • 2020-06-08
      • 2015-03-02
      • 2015-05-08
      • 2014-01-25
      • 1970-01-01
      相关资源
      最近更新 更多