1

I have a csv file Temp.csv that I am copying over to another csv file Final.csv using the following piece of code.

dirname = os.path.dirname(os.path.abspath(__file__))
csvfilename = os.path.join(dirname, 'Final.csv')
tempfile = os.path.join(dirname, 'Temp.csv')
with open(csvfilename, 'wb') as output_file:
    writer = csv.writer(output_file, delimiter=',')
    writer.writerow(["Title","This"])
    writer.writerow([])
    with open(tempfile, 'r') as data_file:
        for line in data_file:
            line = line.replace('\n', '')
            row = line.split(",")            
            writer.writerow(row)

The last 5 lines of the code are going to write to the Final.csv file from column A. I want them to write to final.csv from column D. Also I have multiple columns in the Temp.csv file. What is the best solution for this? Using python 2.7.10.

2
  • Did you solve your problem? Commented Oct 17, 2016 at 14:41
  • No. You did! Thank you! Commented Oct 18, 2016 at 8:12

2 Answers 2

1

EDIT Answering comment. You just need to prepend 3 empty strings to your row

dirname = os.path.dirname(os.path.abspath(__file__))
csvfilename = os.path.join(dirname, 'Final.csv')
tempfile = os.path.join(dirname, 'Temp.csv')
with open(csvfilename, 'wb') as output_file:
    writer = csv.writer(output_file, delimiter=',')
    writer.writerow(["Title","This"])
    writer.writerow([])
    with open(tempfile, 'r') as data_file:
        for line in data_file:
            line = line.replace('\n', '')
            row = line.split(",")
            # there are better ways to do this but this is the most straight forward
            new_row = ['', '', '']
            new_row.extend(row)            
            writer.writerow(new_row)
Sign up to request clarification or add additional context in comments.

5 Comments

This is gonna write temp file to Final file from column 4 i.e. the first three columns of the temp file aren't written and not write to the final file from column 4. I want that in the final file the first 3 columns are blank and the entire temp file writes from the fourth column.
Can you please suggest something for that?
@amazingCodingExperience Sorry can't understand your english. Give some examples please, this may help.
e.g. my temp file contains 4 fields in the first four columns Field1, Field2, Field3, Field4. When I write the temp file to the final file, it should leave the first three columns (A,B,C) blank and write Field1 in column D, Field2 in E, Field3 in F and Field4 in G. Hope this helps!
@amazingCodingExperience see changed answer
0

You can try :

with open(tempfile, 'r') as data_file:
   for line in data_file:
       line = line.replace('\n', '')
       row = line.split(",")         
       csvfilename.writerow([row[0]+","+row[3]])

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.