简体   繁体   English

从 a.csv 文件中读取每个字符串

[英]Reading each string from a .csv file

for this assignment, I have to read information from a passwords.csv file.对于这个作业,我必须从 passwords.csv 文件中读取信息。 Here is the content below.这是下面的内容。

March2021 iudj8&neB09 MyDogWo0f mv3m959
ven1vid1v1c1Iulius 123456789 abcdefgcijk What#ItN0w
windsofchange HelloMyNameIs 4584208 4586300
Anniv&0629 BDay@1955 BigBadW0d! !Rf0donotcare
R85r!4L145 S3att!eWa SONameHere Abc!23

In my program, this is what I have.在我的程序中,这就是我所拥有的。

userFile = input("Enter the full pathway file: ")
openFile = open(userFile,"r")
readFile = openFile.read()

newList = []
for line in readFile:
    newList.append(line.strip())
    print(newList)

The output is this. output 是这个。

['M']
['M', 'a']
['M', 'a', 'r']
['M', 'a', 'r', 'c']
['M', 'a', 'r', 'c', 'h']
['M', 'a', 'r', 'c', 'h', '2']
['M', 'a', 'r', 'c', 'h', '2', '0']
['M', 'a', 'r', 'c', 'h', '2', '0', '2']

How can I make it copy the whole string instead of the letter repeated over and over again?我怎样才能让它复制整个字符串而不是一遍又一遍地重复的字母?

You can iterate over the file to read each line and add words:您可以遍历文件以读取每一行并添加单词:

userFile = input("Enter the full pathway file: ")

newList = []
with open(userFile) as f:
    for line in f:
        newList += line.split()

print(newList)

The problem is that you read the file into readFile and then iterate that string.问题是您将文件读入readFile然后迭代该字符串。 Strings iterate character by character so you see the list growing by one character on each print.字符串逐个字符地迭代,因此您可以看到列表在每次打印时增加一个字符。 You should do the iteration on the file, which reads line by line.您应该对文件进行迭代,该文件逐行读取。

But its better to use the csv module in the standard library.但最好使用标准库中的csv模块。 Here, I just used the default "excel" rules but changed the separator from comma to space.在这里,我只是使用了默认的“excel”规则,但将分隔符从逗号更改为空格。 If any of your passwords.csv cells contain properly escaped spaces, they will be processed properly here.如果您的任何密码。csv 单元格包含正确转义的空格,它们将在此处正确处理。

import csv

userFile = input("Enter the full pathway file: ")
with  open(userFile, newline="") as file:
    newList = list(csv.reader(file, delimiter=" "))
print(newList)

Python is "batteries included" - might as well use what is already there. Python 是“包括电池” - 不妨使用已经存在的东西。

You are trying to create a new line after every word (I think) but in your loop您正在尝试在每个单词之后创建一个新行(我认为)但在您的循环中

for line in readFile:
    newList.append(line.strip())
    print(newList)

You're making a new line after every letter.你在每封信后换一个新行。 To make the array, then print the line outside of the loop.要制作数组,然后在循环外打印该行。

for line in readFile:
    newList.append(line.strip())
print(newList)

The problem is because read the entire file into a single string, you're iterating over the character is it.问题是因为将整个文件读入单个字符串,您正在迭代字符就是它。 To split that string up into a list of separate lines, all you need to do it call the str.splitlines() method.要将字符串拆分为单独的行列表,您只需调用str.splitlines()方法即可。

with open(userFile) as openFile:
    newList = openFile.read().splitlines()

print(newList)

I believe your problem is that you have the print statement inside of the for loop.我相信你的问题是你在 for 循环中有 print 语句。 So hopefully所以希望

for line in readFile:
    newList.append(line.strip())
print(newList)

should work.应该管用。 If this isn't what you meant, just tell me.如果这不是你的意思,请告诉我。

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

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