简体   繁体   中英

Copying a specific column from a csv file to another csv in a specific place

I have tried different methods in Python 2.7 that I saw on this forum for copying a specific column from a csv file to another csv file in a specific place(column).

csv1:
Header1 Header2 Header3 Header4
1       2       3       4
1       2       3       4
1       2       3       4
1       2       3       4

csv2:
Header5 Header6 Header7
5       6       7
5       6       7
5       6       7

So I want to copy the column Header2 over the column Header6 resulting the following

csv2:
Header5 Header2 Header7
5       2       7
5       2       7
5       2       7
        2

Every header in in a different cell. I have tried the following(even making a third file) but did not succeeded:

with open('book1.csv', 'r') as book1:
    with open('book2.csv', 'r') as book2:
        reader1 = csv.reader(book1, delimiter=',')
        reader2 = csv.reader(book2, delimiter=',')

        both = []
        fields = reader1.next() # read header row
        reader2.next() # read and ignore header row
        for row1, row2 in zip(reader1, reader2):
            row2.append(row1[-1])
            both.append(row2)

        with open('output.csv', 'w') as output:
            writer = csv.writer(output, delimiter=',')
            writer.writerow(fields) # write a header row
            writer.writerows(both)

Any ideas? :)

The lists that you are appending get appended horizontally. That row has no way of knowing whether or not the next item to be appended to it belongs to the adjacent column or multiple columns over.

The way around this is to identify the length of your Header Column with the most values ( Maximum Column Length among all Columns. )

In your desired "csv2" output, Header2 has the highest number of values in its column ( 4 values ) compared to the other Headers ( 3 values ).

What you want to do is make sure all the other headers have a length equal to the maximum length ( 4 values ).

You can continually add an irrelevant item to each column so that it spaces out perfectly for the next column. Example items you can append vertically to the short columns at the bottom can be an Empty String Value ( "" ) , a Not-Applicable String Value ( "NA" ), or a number like 0 that you don't expect to show up as an integer in any of your data set columns.

try something like:

        for row1, row2 in zip(reader1, reader2):
            newRow = str(row1[0])+","+str(row1[1])+","+str(row1[2])
            both.append(row2)

Also I would suggest you not to copy some other code and paste it as your solution. I would suggest you to try atleast executing a part of code and ask for help. It is fine if you do not add any code too but if you do not try the code you gave it might confuse people and they can't help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM