0

This is my csv Input

,,A01.0 Брюшной тиф
0,"Тиф брюшной, Паратиф, Тиф, Паратифозная лихорадка",

output that I get after my code.

0,

this what I want to have and I have 11000 csv files like this one

"Тиф брюшной, Паратиф, Тиф, Паратифозная лихорадка",A01.0 Брюшной тиф
import csv

data = []
with open('/Users/gfidarov/Desktop/Python/CSV/synonims copy/syn8.csv') as inf:
    reader = csv.reader(inf)
    for row in reader:
        data.append(row)

with open('/Users/gfidarov/Desktop/Python/CSV/merged/syn.csv', 'w') as outf:
    writer = csv.writer(outf)
    writer.writerow([data[1][0], data[0][1]])
George
  • 23
  • 7
  • are the two commas always in the front of the first line? –  Nov 29 '19 at 15:59
  • @SvetlanaofVodianova yes they are because there is three columns – George Nov 29 '19 at 16:04
  • @SvetlanaofVodianova is it even possible to do? – George Nov 29 '19 at 16:06
  • Yes, check for the 2 commas in the first line and slice them out, and then append it to the next line –  Nov 29 '19 at 16:07
  • actually I don't need them, just two columns with names inside that's it ,first, second, something like this. I am somewhere close to that but i am new in python that's why I am confused a bit – George Nov 29 '19 at 16:08
  • @SvetlanaofVodianova yes I can slice them but not in all 11000 csv files only in one – George Nov 29 '19 at 16:11
  • @George ***"how can do that with the entire folder "***: Read [import-multiple-csv-files](https://stackoverflow.com/questions/20906474/import-multiple-csv-files-into-pandas-and-concatenate-into-one-dataframe) – stovfl Nov 29 '19 at 17:01

2 Answers2

1

Question Concat 2 Rows of data and write selected columns

import csv, io

CSV = """,,A01.0 Data1
0,"Sentence 1",
,,A02.0 Data2
0,"Sentence 2",
,,A03.0 Data3
0,"Sentence 3",
"""

data = []
with io.StringIO(CSV) as inf:
    data = [record for record in csv.reader(inf)]

# concat every ziped pair of 2 lists into one list
data2 = [r[0] + r[1] for r in zip(data[0::2], data[1::2])]

with io.StringIO() as out_fh:
    writer = csv.writer(out_fh)
    for rec in data2:
        # Write only Column 4 and 2
        writer.writerow((rec[4], rec[2]))

    print(out_fh.getvalue())

Output:

Sentence 1,A01.0 Data1    
Sentence 2,A02.0 Data2
Sentence 3,A03.0 Data3
stovfl
  • 14,998
  • 7
  • 24
  • 51
0
import csv


cols_to_remove = [0]# Column indexes to be removed (starts at 0)

cols_to_remove = sorted(cols_to_remove, reverse=True)# Reverse so we remove from the end first
row_count = 0# Current amount of rows processed

input_file = ('/Users/gfidarov/Desktop/Python/CSV/synonims copy/syn13.csv')
output_file = ('/Users/gfidarov/Desktop/Python/CSV/merged/syn.csv')
with open(input_file, "r") as source:
    reader = csv.reader(source)
    with open(output_file, "w", newline='') as result:
        writer = csv.writer(result)
        for row in reader:
            row_count += 1
            print('\r{0}'.format(row_count), end='') # Print rows processed
            for col_index in cols_to_remove:
                del row[col_index]
            writer.writerow(row)

with this code I can delete only one csv but I can't figure our how can do that with the entire folder

George
  • 23
  • 7
  • If you're dealing with folders, then the `path` module can help you. Loop though each folder, find the `csv` file, open it and do the operation. –  Nov 29 '19 at 16:33
  • @SvetlanaofVodianova okay but how can I do that in code? – George Nov 29 '19 at 16:36
  • import the `path` module from `pathlib`, use the folder you have your `csv` files in as the root, and loop through it with `iterdir`, open and perform the operation. I don't know how you have your folders structured or your files setup, this is why I can't provide a answer. –  Nov 29 '19 at 16:39