是否可以在Python中嵌套"for"语句?

你好,
我是Python的新手,我想知道是否有可能筑巢"的陈述?
我从.csv文件中的列上运行简单计算的某个问题引起的问题.我想从现有.CSV文件中读取列,并从.CSV文件中写出选定的列以及包含来自计算结果到新.CSV文件的结果的新列.
我正在考虑这样做的方法是嵌套语句,以便我首先阅读所需的列,在列上执行IF else语句,然后使用另一个语句写出结果.
也许我认为这比必须复杂.就像我说的那样,我是新手...我可能错过了关键的一步.这是我到目前为止所拥有的:

选择 | 换行 | 行号
  1. #---Feed TranPy the csv and sys modules---
  2. import csv, sys
  3. #---Open source file, read source file, write to new source file---
  4. source = csv.reader(open("H:/transpor/Transit Ridership Database/TranPy/LoadData.csv", "rb"))
  5. for TIME, DIR, LOCATION, ON, OFF, LOAD, RUNTIME, LATITUDE, LONGITUDE, SAMPLES in source:
  6.     print TIME, DIR, LOCATION, LOAD
  7. for LOAD in source:
  8.     if LOAD>20 
  9.     print 'greater than 20'
  10. else: 
  11.     print 'less than 20'
  12. results = csv.writer(open("H:/transpor/Transit Ridership Database/TranPy/LoadData_out.csv", "wb")) 
  13. results.writerows(row)
  14.  

1)我在"如果加载> 20"处遇到无效的语法错误
2)如何将此计算传递给结果,以便根据其值在列负载中为每行的"大于20/小于20"的语句打印出来?
谢谢您的帮助!

# 回答1


你好,
我不熟悉CSV文件,但我会看到我可以为您提供帮助的内容.
很好,那不是太难了.在IF之后您需要一个结肠,必须缩进打印声明,这样:

选择 | 换行 | 行号
  1. for LOAD in source:
  2.     if LOAD>20:
  3.         print 'greater than 20'

我认为循环的第二个不起作用,因为您不会仅获得负载列.我认为循环将在源中查看所有列并将其称为负载.如何将代码放在第一个循环内部的第二个循环中,这样的循环如何:

选择 | 换行 | 行号
  1. for TIME, DIR, LOCATION, ON, OFF, LOAD, RUNTIME, LATITUDE, LONGITUDE, SAMPLES in source:
  2.     print TIME, DIR, LOCATION, LOAD
  3.     if LOAD>20:
  4.         print 'greater than 20'
  5.     else:
  6.         print 'less than 20'

您应该能够通过嵌套循环通过负载进行迭代,例如:

选择 | 换行 | 行号
  1. for TIME, DIR, LOCATION, ON, OFF, LOAD, RUNTIME, LATITUDE, LONGITUDE, SAMPLES in source:
  2.     print TIME, DIR, LOCATION, LOAD
  3.     for row in LOAD:
  4.         if row>20:
  5.             print 'greater than 20'
  6.         else:
  7.             print 'less than 20'

希望这可以帮助.

# 回答2


谢谢Boxfish ...
但是,似乎Python并未自动"识别"'负载'的值是整数.我在其他论坛上做了更多的挖掘和问题,并且共识似乎是我首先需要以矩阵格式识别CSV的值,将其定义为列表,然后定义LST [5]的值[5]作为整数.然后,我可以运行"大于少于"的"大于"(lst [5])上的语句

选择 | 换行 | 行号
  1. import csv
  2.  
  3. sourcefile = open('H:/example.csv', 'rb')
  4. outfile = open('H:/outfile.csv', 'wb')
  5.  
  6. source = csv.reader(sourcefile, dialect='excel') #tells the program what format sourcefile is in
  7. dest = csv.writer(outfile, dialect='excel') #tells the program what format the destination file is in
  8.  
  9. for lst in source:  #define csv file as a matrix, Python starts at zero.  Columns are skipped, hence, LOAD is column 5, not column 3
  10.     print lst[0], lst[1], lst[2], lst[5]  # TIME, DIR, LOCATION, LOAD
  11.  
  12.     try: 
  13.         x = int(lst[5])     #define LOAD as an integer
  14.         if x >= 20:
  15.             print 'load greater than 20'
  16.             dest.writerow(lst)
  17.         else:
  18.             print 'load less than 20 -- not copied'
  19.  
  20.     except ValueError:   #error handling...if value is null or not in the correct format
  21.         print 'Cannot decide:', repr(lst)    #returns list as a parseable object
  22.  
  23. sourcefile.close() #close the sourcefile
  24. outfile.close() #close the write-to file
  25.  

标签: python

添加新评论