将下面python3代码转换为python2 filePath = "E:/file/file/Investigation/H-Material/UMAT/CN3/" with open(filePath+"Optdata000.csv") as file: for line in file.readlines(): print(line) with open(filePath+"Optdata000.csv") as f_input: csv_input = csv.DictReader(f_input) for row in csv_input: id = row['$id'] para1 = row['para1'] para2 = row['para2'] para3 = row['para3'] para4 = row['para4'] para5 = row['para5'] para6 = row['para6'] para7 = row['para7'] para8 = row['para8'] para9 = row['para9'] print(id, para1, para2, para3, para4, para5, para6, para7, para8, para9)
时间: 2024-04-18 21:26:01 浏览: 104
以下是将Python 3代码转换为Python 2的版本:
import csv
filePath = "E:/file/file/Investigation/H-Material/UMAT/CN3/"
with open(filePath+"Optdata000.csv") as file:
for line in file.readlines():
print(line)
with open(filePath+"Optdata000.csv") as f_input:
csv_input = csv.DictReader(f_input)
for row in csv_input:
id = row['$id']
para1 = row['para1']
para2 = row['para2']
para3 = row['para3']
para4 = row['para4']
para5 = row['para5']
para6 = row['para6']
para7 = row['para7']
para8 = row['para8']
para9 = row['para9']
print id, para1, para2, para3, para4, para5, para6, para7, para8, para9
注意:在Python 2中,print语句不需要使用括号。并且,在Python 2中,csv模块中的DictReader返回的是字典而不是字典视图对象。
阅读全文