rdn = csv.reader(fhand, delimiter=';') for i in rdn: print(', '.join(i))
Вывело ошибку:
Enter a file name: text.csv
avr, atv, bmv
6246.09375, 55856.25, 38.625
6230.859375, 55856.25, 38.625
6230.859375, 55856.25, 38.625
6213.28125, 55856.25, 38.625
6213.28125, 55856.25, 38.625
6195.703125, 55856.25, 38.625
6195.703125, 55856.25, 38.625
6179.296875, 55912.5, 42
6179.296875, 55912.5, 42
6162.890625, 55912.5, 42
6162.890625, 55912.5, 42
6146.484375, 55912.5, 42
6146.484375, 55912.5, 42
6128.90625, 55912.5, 42
6128.90625, 55912.5, 42
6111.328125, 55987.5, 45.625
6111.328125, 55987.5, 45.625
6094.921875, 55987.5, 45.625
6094.921875, 55987.5, 45.625
6077.34375, 55987.5, 45.625
6077.34375, 55987.5, 45.625
6060.9375, 55987.5, 45.625
6060.9375, 55987.5, 45.625
6043.359375, 56062.5, 49.25
6043.359375, 56062.5, 49.25
6026.953125, 56062.5, 49.25
6026.953125, 56062.5, 49.25
6010.546875, 56062.5, 49.25
6010.546875, 56062.5, 49.25
———————————————————
If you want to EXIT this program then type - done
Enter the required parameter: bmv
Traceback (most recent call last):
if not (param in title):
NameError: name ‘title’ is not defined
Добавила строчки кода, чтобы ещё раз перематывать через fhand.seek(0):
import csv import string fname = input("Enter a file name: ") outputfile = "out.csv" try: fhand = open(fname, mode='r', newline='') fcopy = open(outputfile, mode='w') except: print ("File don’t exist: ", fname) exit() rdn = csv.reader(fhand, delimiter=';') for i in rdn: print(', '.join(i)) fhand.seek(0) rdn = csv.reader(fhand, delimiter=';') for title in list(rdn)[:1]: print("This file has parameters: ") print(title) ''' rdn = csv.reader(fhand, delimiter=';') i = ' '.join(list(rdn)[0]) print(i) ''' while True: print("---------------------------------------------------------") print("If you want to EXIT this program then type - done") param = input("Enter the required parameter: ") if param == "done": break if not (param in title): print("This parameter doesn't exist. Please try again") exit() rdn = csv.reader(fhand, delimiter=';') print("This file has parameters: ") print(title) titleIndex = title.index(param) print(titleIndex) fhand.seek(0) wrtr = csv.writer(fcopy, dialect='excel') for row in rdn: wrtr.writerow(row) print("---------------------------------------------------------") fhand.close() fcopy.close()
Но записывает в out.csv также как и в первый раз при введении только одного параметра:
avr,atv,bmv
6246.09375,55856.25,38.625
и т.д.