1

Hi I am using this method to write a csv file from a csv file which is a hashed code but i only receive the last row in output, how can i add each row to the previous one?

import hashlib
import csv
d = dict()
result = ()
for i in range(0 , 9999) :
    n = hashlib.sha256(str(i).encode())
    d[n.hexdigest()] = str(i)
with open('/Users/MJ-Mac/Desktop/karname.txt') as f:
    file = csv.reader(f)
    for row in file :
        a = row[0]
        b = d[row[1]]
        result = (a , b)
        with open('/Users/MJ-Mac/Desktop/result3.txt', 'w') as f2:
            file2 = csv.writer(f2)
            file2.writerow(result)
3
  • 2
    instead of 'w', use 'a' for appending Commented Nov 26, 2021 at 17:33
  • @omuthu csv.writer wirks fine with 'w' as long as there is only one instance of csv.writer. Changing the code so there's just one instance is more effective Commented Nov 26, 2021 at 17:46
  • @Scrapper142 Right, I missed that the writer is part of a loop where multiple instances are open Commented Nov 26, 2021 at 17:51

4 Answers 4

1

Other answers have suggested replacing 'w' with 'a', this is not necessary when working with csv.writer. It also could make your file grow everytime you run the program.

Instead of reopening and closing relut3.txt, keep it open and use just one writer

import hashlib
import csv
d = dict()
result = ()
for i in range(0 , 9999) :
    n = hashlib.sha256(str(i).encode())
    d[n.hexdigest()] = str(i)
with open('/Users/MJ-Mac/Desktop/result3.txt', 'w') as result_file:
    result_writer = csv.writer(result_file)  # only create this once
    
    with open('/Users/MJ-Mac/Desktop/karname.txt') as f:
        file = csv.reader(f)
        for row in file :
            a = row[0]
            b = d[row[1]]
            result = (a , b)

            result_writer.writerow(result)  # use the already created writer
Sign up to request clarification or add additional context in comments.

Comments

0

Your code is writing one line to the file, then it opens the file again and writes the next line as the full content of the program. So on the second run through the loop only the second line will be in the file.

import hashlib
import csv
d = dict()
result = ()
for i in range(0 , 9999) :
    n = hashlib.sha256(str(i).encode())
    d[n.hexdigest()] = str(i)
with open('/Users/MJ-Mac/Desktop/karname.txt') as f:
    file = csv.reader(f)
    for row in file :
        a = row[0]
        b = d[row[1]]
        result = (a , b)
        with open('/Users/MJ-Mac/Desktop/result3.txt', 'a') as f2:
            file2 = csv.writer(f2)
            file2.writerow(result)

It might be better to open the file and then write everything to it:

import hashlib
import csv
d = dict()
result = ()
for i in range(0 , 9999) :
    n = hashlib.sha256(str(i).encode())
    d[n.hexdigest()] = str(i)
with open('/Users/MJ-Mac/Desktop/karname.txt') as f:
    file = csv.reader(f)
    with open('/Users/MJ-Mac/Desktop/result3.txt', 'w') as f2:
        file2 = csv.writer(f2)
        for row in file :
            a = row[0]
            b = d[row[1]]
            result = (a , b)
            
            file2.writerow(result)

Comments

0

I can't run this myself, since your data is not included.

However, I think your problem is that with open('/Users/MJ-Mac/Desktop/result3.txt', 'w') has the "w" flag -- which stands for "write" -- so your data is being overwritten, You might instead need the "a" flag for "append," so that each line will be appended to the data you are exporting.

import hashlib
import csv
d = dict()
result = ()
for i in range(0 , 9999) :
    n = hashlib.sha256(str(i).encode())
    d[n.hexdigest()] = str(i)
with open('/Users/MJ-Mac/Desktop/karname.txt') as f:
    file = csv.reader(f)
    for row in file :
        a = row[0]
        b = d[row[1]]
        result = (a , b)
        with open('/Users/MJ-Mac/Desktop/result3.txt', 'a') as f2:
            file2 = csv.writer(f2)
            file2.writerow(result)

Comments

0

It is easier and more readable to open both the input and output files at once, and initialise the CSV reader and writer at the start:

with open('/Users/MJ-Mac/Desktop/karname.txt') as in_file, open('/Users/MJ-Mac/Desktop/result3.txt', 'w') as out_file:
    output = csv.writer(out_file)
    for row in csv.reader(in_file):
        output.writerow([row[0], d[row[1]])
 

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.