![](/img/trans.png)
[英]I have an array of strings. I want to print those that start with the word New.
[英]I'm new to coding, after finding the maximum value float in the csv file, I want to print the whole row, which includes strings. I'm using python
import csv
with open('Prenoms.csv', 'r') as f: # open file variable f
reader = csv.reader(f) # read file
next(reader) # skip the first line
answer = max(float(column[3].replace(',', '')) for column in reader)
print(answer) # show max value
#print("hi") # Debugging line of code (works)
float_value = answer
string_value = str(float_value) # translate from float to string
f = open("Prenoms.csv","r") # Open file for read only
c = csv.reader(f, delimiter=',') # Read the file using the
# assigned delimiter
while row in c:
if answer == row[3]:
print(row)
else:
print("hi i'm here!") # while loop checks unlit list ends
for row in c: # for loop checks for 11629 times
if answer == row[3]: # if conditional to check if the
#row contains the desired float (max)
print(row) # Doesn't print but no errors are shown
print("Hello World") # This is a debugging line of
# code but it doesn't print
f.close()
#print(row) # This prints wrong row
#print("Hello World") # Debugging line of code (works)
这比你想象的要简单得多:
with open('Prenoms.csv', 'r') as f:
prenoms = csv.reader(f)
next(prenoms)
currentmax = 0.0
therow = ''
for row in prenoms:
newval = float(row[3])
if newval > currentmax:
currentmax = newval
therow = row
print(f'row {therow} contains the max val {maxval}')
我无法测试它,因为我没有你的文件。 可能会出现问题,因为一旦您假设 ',' 不是分隔符(您在内容中替换它!),但稍后您重新打开同一个文件并将分隔符设置为 ','
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.