Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import csv
- def print_csvfile(csvfile, _delimiter, _quotechar):
- tmp = csv.reader(csvfile, delimiter=_delimiter, quotechar=_quotechar)
- for row in tmp:
- print(', '.join(row))
- csvfile.seek(0)
- def search_csvfile(csvfile, search_name):
- tmp_dict = dict()
- tmp_dict = csv.DictReader(csvfile)
- for row in tmp_dict:
- if row['Country Name'] == search_name:
- return row
- return -1
- list_name_keys = ['п»їSeries Name', 'Series Code', 'Country Name', 'Country Code', '2019 [YR2019]']
- csvfile = 0
- search_row = -1
- try:
- csvfile = open('Data.csv', 'r', newline='')
- except IOError:
- print("ERROR: File could not be opened")
- else:
- print_csvfile(csvfile, ',', '"')
- search_country = input("Enter search country: ")
- search_row = search_csvfile(csvfile, search_country)
- if search_row != -1:
- print(search_row)
- else:
- print(f"Country {search_country} can not be found")
- finally:
- csvfile.close()
- try:
- csvfile = open('Data_res.csv', 'a', newline='')
- except IOError:
- print("ERROR: File could not be opened")
- else:
- write = csv.DictWriter(csvfile, fieldnames=list_name_keys, delimiter=';', quotechar='|')
- if search_row != -1:
- write.writerow(search_row)
- finally:
- csvfile.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement