Chad Leftwich
Chad Leftwich

Reputation: 33

Python Code to Copy lines from One CSV to Another and Add Column

I'm trying to use Python to copy lines from one csv file to another and add data to a new column in the process. The data is being copied correctly to the new file, but it's all being copied to the same line in the new file.

file = "C:/original_file.csv"
nf = "C:/file_updated.csv"

i = 0

with open(file, 'r') as origFile:
    with open(nf, 'w') as newFile:

        lineList = []
        for line in origFile:
            strippedLine = line.strip()
            lineList = strippedLine.split(',')

            lineList.append("C:/ATT" + str(i) + "_PHOTO 1.jpg")
            lineStr = str(lineList)
            lineStr = lineStr.replace("'", "")

            newFile.write(lineStr)
            print lineList
            i += 1

origFile.close()
newFile.close()

How can I make it so that each line from the first file copies to a separate line of the new file?

Upvotes: 0

Views: 2714

Answers (3)

norie
norie

Reputation: 9857

If you want to do it without any imports you could try something like this which adds a new column with the header New Field.

Of course it assumes the original CSV has a header row.

file = "original_file.csv"
nf = "file_updated.csv"

with open(file, 'r') as origFile:
    data = [line.strip().split(',') for line in origFile.readlines()]

header = data[0]
data = data[1:]
header.append('New Field')       
data = [line + [f'C:/ATT{idx}_PHOTO 1.jpg'] for idx, line in enumerate(data)]
data = [','.join(line) for line in [header]+data]
        
with open(nf, 'w') as newFile:
    newFile.writelines('\n'.join(data))

"""
SAMPLE INPUT
Field1,Field2
Data1,Data2
Data3,Data4


SAMPLE OUTPUT
Field1,Field2,New Field
Data1,Data2,C:/ATT0_PHOTO 1.jpg
Data3,Data4,C:/ATT1_PHOTO 1.jpg
"""

Upvotes: 0

Ben
Ben

Reputation: 6348

No need to install pandas, the built-in csv library is great for this!!

$ cat tmp.csv
first,second
third,fourth
import csv

to_read = "./tmp.csv"
to_write = "./tmp2.csv"

with open(to_read, newline="") as to_read_fp, open(to_write, "w", newline="") as to_write_fp:
    reader = csv.reader(to_read_fp)
    writer = csv.writer(to_write_fp)

    for count, row in enumerate(reader):
        row.append(f"C:/ATT{count}_PHOTO 1.jpg")
        writer.writerow(row)
$ cat tmp2.csv
first,second,C:/ATT0_PHOTO 1.jpg
third,fourth,C:/ATT1_PHOTO 1.jpg

Upvotes: 0

marckesin
marckesin

Reputation: 71

file = "C:/original_file.csv"
nf = "C:/file_updated.csv"

i = 0

with open(file, 'r') as origFile:
    with open(nf, 'w') as newFile:

        lineList = []
        for line in origFile:
            strippedLine = line.strip()
            lineList = strippedLine.split(',')

            lineList.append("C:/ATT" + str(i) + "_PHOTO 1.jpg")
            lineStr = str(lineList)
            lineStr = lineStr.replace("'", "")

            newFile.write(lineStr)
            newFile.write('\n') #Insert a new line
            print lineList
            i += 1

origFile.close()
newFile.close()

Upvotes: 2

Related Questions