[Tutor] Instead of *.TXT I would like to use *.CSV exported from Excel

Mario Py mariopy at gmx.com
Fri Jul 4 02:37:38 CEST 2014


Windows 7, Python 3.4.0

I have a simple translation refresher program that works.
For input I'm using TXT file, (prevedi.txt) words in it are separated by 
coma that I'm editing, adding manually. (adding new 'refresher' words as 
needed) And program works:

from random import shuffle

print('Write translation of Slovene word ')
print()

with open('c:\\prevedi.txt', 'r', encoding='utf8') as f:
     lines = f.readlines()

shuffle(lines)

for line in lines:
     question, rightAnswer = line.strip().split(',')
     answer = input(question + ' ')
     if answer.lower() != rightAnswer:
         print('Correct is: %s.' % rightAnswer,)
         print()

I would like to be able to add more 'refresher' words in Excel instead 
of TXT fine and then export it to CSV file from Excel. I don't know how 
to adjust the code for this.

 From my tries you can see, I don't have a clue about programing.
I'm searching the internet and inserting pieces of code I found with the 
hope that program would work. Whatever I do, I get different error trace 
backs, too many to post here.

I know is it simple, trivial task but I'm just not up to it.
Can anyone help me please?

This is what I'm trying:

from random import shuffle
import csv

print('Write translation of Slovene word ')
print()

with open('c:\\prevedi.csv', 'r', newline='', encoding='utf8') as f:
# with open('c:\\prevedi.csv', 'r', newline='', encoding='utf8') as csv: 
tried this one, didn't work
     lines = f.readlines()
     # lines = csv.readlines(f) - this one doesn't work as well
     # lines = cvs.readlines() - neither this one...

shuffle(lines)

for line in lines:
     question, rightAnswer = line.strip().split(',')
     answer = input(question + ' ')
     if answer.lower() != rightAnswer:
         print('Correct is: %s.' % rightAnswer,)
         print()

I have a feeling, there is a problem with that 'as f:' part

Also, I probably don't need that strip part, csv import probably deals 
with it, but I don't know how much or which part to delete out...
Any time I try to change something, new trace error appears...
I'm out of ideas and tries... Who said even blind squirrel... ?

Thank you for helping me out,
Mario


More information about the Tutor mailing list