0

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?

3
  • I think this is something that you could do effortlessly with pandas. Is there any reason you want to use only python? Commented Jul 14, 2021 at 15:47
  • The only reason was a lack of experience with pandas. But I will look into it. Commented Jul 14, 2021 at 15:50
  • If you want to solve it with pandas then I would suggest you to look at the solution of this: stackoverflow.com/questions/58274401/… . Then there is a simple save functionality to csv. Commented Jul 14, 2021 at 16:11

3 Answers 3

2
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()
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! That was exactly what I needed.
0

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

Comments

0

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
"""

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.