(Sep-18-2022, 02:41 AM)Pedroski55 Wrote: [ -> ]Without pandas:
import csv
import glob
path = '/home/pedro/myPython/csv/csv/'
csvs = glob.glob(path + '*.csv')
for file in csvs:
print('The csv files are:', file)
while True:
myfile = input('Copy and paste the whole path to the file you want here, enter q to quit ... ')
if myfile == 'q':
break
files.append(file)
def getData(acsv):
with open(acsv) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
data = []
for row in csv_reader:
data.append(row)
return data
data1 = getData(files[0])
data2 = getData(files[1])
data3 = getData(files[2])
# check if the first 4 rows are the same in all files
# otherwise could be a problem
for i in range(4):
if not data1[i] == data2[i]:
print('This line', i, 'is not the same in data1 and data2')
# data1 and data2 are the same in rows 1 to 4
for i in range(4):
if not data2[i] == data3[i]:
print('This line', i, 'is not the same in data1 and data2')
# the first 4 rows seem to be the same
# write the all rows of data1, start at row 5 for data2 and data3
with open(path + 'merge_me.csv', 'w') as file:
for i in range(0, len(data1)):
data1_string = ','.join(data1[i])
file.write(data1_string + '\n')
for i in range(5, len(data2)):
data2_string = ','.join(data2[i])
file.write(data2_string + '\n')
for i in range(5, len(data3)):
data3_string = ','.join(data3[i])
file.write(data3_string + '\n')
# test it
with open(path + 'merge_me.csv') as file:
csv_reader = csv.reader(file, delimiter=',')
for row in csv_reader:
print(row)
print('Loooks OK!')
so cool, help fixed it, move files array variable into read CSVs, it is worked, thanks!!
import csv
import glob
files=[]
path = '/home/pedro/myPython/csv/csv/'
csvs = glob.glob(path + '*.csv')
for file in csvs:
files.append(file)
print('The csv files are:', file)
while True:
myfile = input('Copy and paste the whole path to the file you want here, enter q to quit ... ')
if myfile == 'q':
break
def getData(acsv):
with open(acsv) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
data = []
for row in csv_reader:
data.append(row)
return data
data1 = getData(files[0])
data2 = getData(files[1])
data3 = getData(files[2])
# check if the first 4 rows are the same in all files
# otherwise could be a problem
for i in range(4):
if not data1[i] == data2[i]:
print('This line', i, 'is not the same in data1 and data2')
# data1 and data2 are the same in rows 1 to 4
for i in range(4):
if not data2[i] == data3[i]:
print('This line', i, 'is not the same in data1 and data2')
# the first 4 rows seem to be the same
# write the all rows of data1, start at row 5 for data2 and data3
with open(path + 'merge_me.csv', 'w') as file:
for i in range(0, len(data1)):
data1_string = ','.join(data1[i])
file.write(data1_string + '\n')
for i in range(5, len(data2)):
data2_string = ','.join(data2[i])
file.write(data2_string + '\n')
for i in range(5, len(data3)):
data3_string = ','.join(data3[i])
file.write(data3_string + '\n')
# test it
with open(path + 'merge_me.csv') as file:
csv_reader = csv.reader(file, delimiter=',')
for row in csv_reader:
print(row)
print('Loooks OK!')
[/quote]