你好,
我是Python的新手,我想知道是否有可能筑巢"的陈述?
我从.csv文件中的列上运行简单计算的某个问题引起的问题.我想从现有.CSV文件中读取列,并从.CSV文件中写出选定的列以及包含来自计算结果到新.CSV文件的结果的新列.
我正在考虑这样做的方法是嵌套语句,以便我首先阅读所需的列,在列上执行IF else语句,然后使用另一个语句写出结果.
也许我认为这比必须复杂.就像我说的那样,我是新手...我可能错过了关键的一步.这是我到目前为止所拥有的:
- #---Feed TranPy the csv and sys modules---
- import csv, sys
- #---Open source file, read source file, write to new source file---
- source = csv.reader(open("H:/transpor/Transit Ridership Database/TranPy/LoadData.csv", "rb"))
- for TIME, DIR, LOCATION, ON, OFF, LOAD, RUNTIME, LATITUDE, LONGITUDE, SAMPLES in source:
- print TIME, DIR, LOCATION, LOAD
- for LOAD in source:
- if LOAD>20
- print 'greater than 20'
- else:
- print 'less than 20'
- results = csv.writer(open("H:/transpor/Transit Ridership Database/TranPy/LoadData_out.csv", "wb"))
- results.writerows(row)
-
1)我在"如果加载> 20"处遇到无效的语法错误
2)如何将此计算传递给结果,以便根据其值在列负载中为每行的"大于20/小于20"的语句打印出来?
谢谢您的帮助!
# 回答1
你好,
我不熟悉CSV文件,但我会看到我可以为您提供帮助的内容.
很好,那不是太难了.在IF之后您需要一个结肠,必须缩进打印声明,这样:
- for LOAD in source:
- if LOAD>20:
- print 'greater than 20'
我认为循环的第二个不起作用,因为您不会仅获得负载列.我认为循环将在源中查看所有列并将其称为负载.如何将代码放在第一个循环内部的第二个循环中,这样的循环如何:
- for TIME, DIR, LOCATION, ON, OFF, LOAD, RUNTIME, LATITUDE, LONGITUDE, SAMPLES in source:
- print TIME, DIR, LOCATION, LOAD
- if LOAD>20:
- print 'greater than 20'
- else:
- print 'less than 20'
您应该能够通过嵌套循环通过负载进行迭代,例如:
- for TIME, DIR, LOCATION, ON, OFF, LOAD, RUNTIME, LATITUDE, LONGITUDE, SAMPLES in source:
- print TIME, DIR, LOCATION, LOAD
- for row in LOAD:
- if row>20:
- print 'greater than 20'
- else:
- print 'less than 20'
希望这可以帮助.
# 回答2
谢谢Boxfish ...
但是,似乎Python并未自动"识别"'负载'的值是整数.我在其他论坛上做了更多的挖掘和问题,并且共识似乎是我首先需要以矩阵格式识别CSV的值,将其定义为列表,然后定义LST [5]的值[5]作为整数.然后,我可以运行"大于少于"的"大于"(lst [5])上的语句
- import csv
-
- sourcefile = open('H:/example.csv', 'rb')
- outfile = open('H:/outfile.csv', 'wb')
-
- source = csv.reader(sourcefile, dialect='excel') #tells the program what format sourcefile is in
- dest = csv.writer(outfile, dialect='excel') #tells the program what format the destination file is in
-
- 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
- print lst[0], lst[1], lst[2], lst[5] # TIME, DIR, LOCATION, LOAD
-
- try:
- x = int(lst[5]) #define LOAD as an integer
- if x >= 20:
- print 'load greater than 20'
- dest.writerow(lst)
- else:
- print 'load less than 20 -- not copied'
-
- except ValueError: #error handling...if value is null or not in the correct format
- print 'Cannot decide:', repr(lst) #returns list as a parseable object
-
- sourcefile.close() #close the sourcefile
- outfile.close() #close the write-to file
-