简体   繁体   English

使用 python 写入 csv 文件

[英]writing into a csv file with python

heres my little program.这是我的小程序。 at the end i want to write the names and passwords into csv file like this:最后,我想将名称和密码写入 csv 文件,如下所示:

Jack,9978
Sara,1647

but i cant?: my program output is correct but when i write it into csv it goes like:但我不能?:我的程序 output 是正确的,但是当我将它写入 csv 时,它就像:

Jack9978,Sara1674

how will you fix it?你将如何解决它?

import hashlib
import csv

answer = []
usr_pas = []

with open('...', 'r') as f:
    reader = csv.reader(f)
    for word in reader:
        usr_pas.append(word)

for i in range(999, 10000):
    num = str(i)
    m = hashlib.sha256()
    m.update(num.encode('utf-8'))
    hsh = m.hexdigest()
    hash_dict = {hsh: num}
    for key in list(hash_dict.items()):
        for value in usr_pas:
            if key[0] == value[1]:
                answer.append(value[0] +','+ key[1])

file = open("...", 'w', newline='')
with file:
    writer = csv.writer(file)
    writer.writerow(i.strip().replace(',', '') for i in answer)
file.close()

what did i wrong??我做错了什么??

Try this (lines with comments are changed):试试这个(更改注释的行):

import hashlib
import csv

answer = []
usr_pas = []

with open('...', 'r') as f:
    reader = csv.reader(f)
    for word in reader:
        usr_pas.append(word)

for i in range(999, 10000):
    num = str(i)
    m = hashlib.sha256()
    m.update(num.encode('utf-8'))
    hsh = m.hexdigest()
    hash_dict = {hsh: num}
    for key in list(hash_dict.items()):
        for value in usr_pas:
            if key[0] == value[1]:
                answer.append(value[0] +','+ key[1] + '\n') #added '\n' at the end

file = open("...", 'w', newline='')
with file:
    writer = csv.writer(file)
    writer.writerow(i for i in answer) #removed i.replace
file.close()

I guess you want a csv file with multiple lines instead of one.我猜你想要一个多行而不是一行的 csv 文件。 If so, my suggestion is to use csv.csvwriter.writerows instead of csv.csvwriter.writerow .如果是这样,我的建议是使用csv.csvwriter.writerows而不是csv.csvwriter.writerow The latter is designed to write a single row.后者旨在写入单行。 See the official document here .请参阅此处的官方文档。 Indeed multiple lines might be created with \n manipulator, it means a single line with multiple elements that contains "new line", which seems awkward.确实可以使用\n操纵器创建多行,这意味着具有多个元素的单行包含“新行”,这看起来很尴尬。

Since we can use the default delimiter (comma), we just need to manage each element in the line as a tuple (or a list).由于我们可以使用默认分隔符(逗号),我们只需要将行中的每个元素作为元组(或列表)进行管理。 Answers should be added into list answer like this:答案应该像这样添加到列表answer中:

answer.append((value[0], key[1]))

while we write rows in this way:当我们以这种方式写行时:

writer.writerows(answer)

Let's put them together:让我们把它们放在一起:

import hashlib
import csv 

answer = []
usr_pas = []

with open('...', 'r') as f:
    reader = csv.reader(f)
    for word in reader:
        usr_pas.append(word)

for i in range(999, 10000):
    num = str(i)
    m = hashlib.sha256()
    m.update(num.encode('utf-8'))
    hsh = m.hexdigest()
    hash_dict = {hsh: num}
    for key in list(hash_dict.items()):
        for value in usr_pas:
            if key[0] == value[1]:
                # answer.append(value[0] +','+ key[1])
                answer.append((value[0], key[1]))

file = open("...", 'w', newline='')
with file:
    writer = csv.writer(file)
    # writer.writerow(i.strip().replace(',', '') for i in answer)
    writer.writerows(answer)
file.close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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