Apr-24-2022, 10:04 AM
Makes a change from doing sudokus!
Maybe like this, very simple:
Maybe like this, very simple:
def myApp(): import csv import glob # maybe use linecache? #import linecache path2csv = '/home/pedro/myPython/csv/' files = glob.glob(path2csv + '*.csv') for f in files: print('The csv files are', f) myfile = input('copy and paste the file you want to modify ... ') with open(myfile) as mf: mylist = mf.readlines() mystring = input('What do you want to put in the columns? Enter a string like AA ... ') # could get the number of columns automatically by splitting mylist[0] and get the len(mylist[0].split(',')) num_cols = input('How many columns of the string do you want? ') num_rows = input('How many rows of columns with the string do you want? ') # make 1 row a_row = '' for i in range(int(num_cols)): if not i == int(num_cols) -1: a_row = a_row + mystring + ',' else: a_row = a_row + mystring + '\n' insert_string = int(num_rows) * a_row headers = mylist[0] new_start = headers + insert_string # get a slice of mylist without the headers new_list = mylist[1:] new_list_string = ''.join(new_list) new_csv_string = new_start + new_list_string with open(path2csv + 'modified_csv.csv', 'w') as nf: nf.write(new_csv_string) # check the result with open(path2csv + 'modified_csv.csv') as infile: result = csv.reader(infile) for line in result: print(line)