I am making a script that combines a bunch of txt files into one csv file, however because I am using the csv package I can only use writerow and not columns. Right now it just makes a csv file with one column and a bunch of rows.
def l_r_file(dirstart):
print(os.listdir())
with open("config.csv", 'w', newline='')as f:
writer = csv.writer(f)
all_dir = os.listdir()
for x in all_dir[:-1]:
os.chdir(dirstart + '\\' + x)
working_file = open('Config.txt', "r")
commaseperated = working_file.readlines()
for i in commaseperated:
writer.writerow([i])
os.chdir(dirstart)
working_file.close()
the way the config.txt files are set up is that its just one column of text with many rows. I am trying to set up the csv page so that I can compare the different txt files. So as an example lets say the first row in config.txt says "Emily", and the first row on another config file says bob, I want to be able to compare the two or more side by side in the csv file. But I opened/saved the strings in the txt files into list. This is a problem because usually csv files wants things in a row on the same list, like
rows = [ ['Nikhil', 'COE', '2', '9.0'],
['Sanchit', 'COE', '2', '9.1'],
['Aditya', 'IT', '2', '9.3'],
['Sagar', 'SE', '1', '9.5'],
['Prateek', 'MCE', '3', '7.8'],
['Sahil', 'EP', '2', '9.1']]
where each list would turn into a single row but because of my data I have them in a list like this
List1 = ['emily', 'randy', 'tom']
List2 = ['bob', 'tom', 'astrid']
because I am reading each txt file and storing it into a list. Here's what my txt file might looks like.
emily
randy
tom
eric
lisa
I'm working with a bunch of txt files with a bunch of rows in each of them too. I feel like I am overthinking this. Please let me know if I am not clear thanks!. Also there is probably a easy way to do this on panda but I am trying to do it without panda.
I have tried combining them into a 2d list like this
combined_lst = [['emily', 'randy', 'tom'], ['bob', 'tom', 'astrid']]
and referencing them using a loop. However, since the txt files have different lengths eg. there are more names in one config file than the other it doesn't work.