0

I'm trying to import the data from X (6 in this case) CSV Files containing some data about texts, and putting one specific row from each Document onto a new one, in such a way that they appear next to each other (export from document 1 on column 1, from the second document on row 2 and so on). I've been unsuccessful so far.

# I have a list containing the path to all relevant files
files = ["path1", "path2", ...]

# then I tried cycling through the folder like this
for file in files:
    with open(file, "r") as csvfile:
        reader = csv.reader(csvfile, delimiter=",")
        for row on reader:
            # I'm interested in the stuff stored on Column 2
            print([row[2]])
# as you can see, I can get the info from the files, but from here 
# on, I can't find a way to then write that information on the 
# appropiate coloumn of the newly created CSV file

I know how to open a writer, what I don't know is how to write a script that writes the info it fetches from the original 6 documents on a DIFFERENT COLUMN every time a new file is processed.

codewario
  • 19,553
  • 20
  • 90
  • 159

1 Answers1

0
# I have a list containing the path to all relevant files
files = ["path1", "path2", ...]
newfile = "newpath1"

# then I tried cycling through the folder like this
for file in files:
    with open(file, "r") as csvfile:
        reader = csv.reader(csvfile, delimiter=",")
        with open(newfile, "a") as wcsvfile:
            writer = csv.writer(wcsvfile)
            for row on reader:
                # I'm interested in the stuff stored on Column 2
                writer.writerow([row[2]])
arunp9294
  • 767
  • 5
  • 15
  • Won't this create a new file for every file I process? I rather want to create a single file where everything is stored. Problem is, if I simply add a write() function on there, the script overwrites the old info with the new info. – Winkler Áron Oct 19 '19 at 16:42
  • https://stackoverflow.com/questions/4706499/how-do-you-append-to-a-file-in-python – arunp9294 Oct 19 '19 at 18:47
  • using open(newfile, "a") will append data. Will not over ride – arunp9294 Oct 19 '19 at 18:48