600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > python将csv文件转换为列表_如何将csv文件数据转换成列表?

python将csv文件转换为列表_如何将csv文件数据转换成列表?

时间:2019-05-22 01:31:25

相关推荐

python将csv文件转换为列表_如何将csv文件数据转换成列表?

仍未解决的部分

我正在尝试转换csv文件中存储的数据'存储.txt'以使我可以更有效地使用它很容易。就像目前,我的文件存储中的项目如下:Leroy,55349234,ok@

Satish,231355,no@

Krut,6969,69@

我想得到上述数据,并将其列为一个列表,如(在选项4中):

^{pr2}$

我试图把它转换成list,所以我想做的基本上是当用户想知道某个特定成员的详细信息时,比如说“Satish”,我希望代码能够在整个文件中找到“Satish”的名称,然后将整行作为一个列表,例如:

['Satish','231355','没有@']

所以现在用户可以输入如果他们想知道他的名字,联系方式或电子邮件id.说用户只想知道他的电子邮件,所以我要做的就是

打印元素[2]。在

已解决

如Jura所建议。

现在,来到我的选择3,(这里是代码):#a=(raw_input("Enter the staff's name: ")).capitalize()

a=(raw_input("Enter the staff's name: ")).capitalize()

with open('store.txt', 'r') as store:

reader = csv.reader(store)

for row in reader:

if row[0]==a:

print row

continue

else:

print "No staff with name %s is found in the database,please try again."%a

break

当我运行程序并选择3时,我得到两个错误:

1) 如果我把名字写为“Leroy”,我会得到:Database

1)Register people

2)View People registered

3)Staff Details

Enter your choice over here: 3

Enter the staff's name: leroy

['Leroy', '55349234', 'ok@']

No staff with name Leroy is found in the database,please try again

但这是不应该发生的,因为档案里已经有了“勒罗伊”存储.txt,那么为什么python也运行else语句并打印“在数据库中找不到名为Leroy的员工,请重试”。

2) 现在当我在“Krut”中tpye时,它会说“在数据库中找不到名为Krut的员工,请再试一次。”存储.txt'

. 在

以下是我的源代码:import csv

print "Database"

print "1)Register people"

print "2)View People registered"

print "3)Staff Details"

choice=input("Enter your choice over here: ")

#This part takes the input from the user and then stores it in 'store.txt'

if choice==1:

a=input("Enter how many people to register: ")

for i in range(0,a) :

a=(raw_input("Enter the name: ")).capitalize()

b=input("Enter contact number: ")

c=raw_input("Enter the email ID: ")

d=raw_input("Enter your address: ")

with open ('store.txt','a') as store:

storeWriter=csv.writer(store)

storeWriter.writerow([a,b,c])

store.close()

#This prints all the data in the file 'store.txt

if choice==2:

with open('store.txt', 'r') as store:

reader = csv.reader(store)

print"- - - - - - - - - - - - - - - - - - - - - - - - - - - - "

for row in reader:

print 'Name:- '+row[0]

print 'Phone Number:- '+row[1]

print 'Email ID:- '+row[2]

print"- - - - - - - - - - - - - - - - - - - - - - - - - - - - "

if choice==3:

#a=(raw_input("Enter the staff's name: ")).capitalize()

a=(raw_input("Enter the staff's name: ")).capitalize()

with open('store.txt', 'r') as store:

reader = csv.reader(store)

for row in reader:

if row[0]==a:

print row

continue

else:

print "No staff with name %s is found in the database,please try again."%a

break

if choice==4:

print "List"

'''

What this program does basically is that it checks the entered name(a).But then if i type the name 'Leroy' it not only prints the

first line but also prints "No staff with name Leroy is found in the database,please try again." which is not required.And now if i

eneter a as Krut, it prints "No staff with name %s is found in the database,please try again." even though there is a name Krut in the

field.How do i get this done (i.e Enter Krut and then it should print row of 'Krut' that is "Krut,5537590,froost1999@"

Also next i want is that (in choice 4) if they eneter the name , it converts the file's(store.txt) data into a listSo basically what

i can do with that would be like print elemenet [0] for name, element[1] for contact number,element[2] for email ID etc.Like it should

make the first line of code that is "Leroy,55349234,ok@" into a list form ['Leroy','55349234','ok@']

so that when the user asks for the contact detail of the specific person i can just print list.(1) to get

their phone number.

'''

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。