1
\$\begingroup\$

I have a csv file

1;(same.)
2;Type...
3; (who are you?)
4; I am a talker whom the world has not yet seen. And you?
5; (leave.)
6;Connect to the Internet - I'll leave...
7; (I would like to.)
8;He would like to! And you're a no-brainer guy!

The objective was to make it look like

row1 Question -  '(same.)' -  answer- 'Type...'
row2 Question -  '(who are you?)' -  answer- 'I am a talker whom the world has not yet seen. And you?

I am just learning, so it was hard for me.

The feedback is much appreciated, how it can be done easier.

final code

list1=[]
list2=[]
with open('answers.csv',mode='r', newline='') as f:
    reader = csv.reader(f, delimiter=';')
    for line in reader:
        num = int(line[0])
        if num % 2:
            list1.append(line)
        if not num % 2:
            list2.append(line)
# print(list1)
# print(list2)
for i,m in zip(list1,list2):
    print(f'Question {i[1]}, Answer {m[1]}')
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Do you want to write back to a file with the new structure, or just use it to display questions and answers on the terminal? \$\endgroup\$
    – Reinderien
    Feb 8, 2022 at 13:18
  • \$\begingroup\$ Terminal is fine, the objective is mainly to combine every 2 lines. \$\endgroup\$ Feb 8, 2022 at 15:53

2 Answers 2

1
\$\begingroup\$

Since you know it will be every other line and start with a Q, I would use Slicing with step parameter, that way you get every n'th item [::n]. Starting at 1 [1::] for the first list, starting at item 2 [2::] in the second list.

with open('answers.csv',mode='r', newline='') as f:
    reader = csv.reader(f, delimiter=';')

for i, m in zip(reader[1::2], reader[2::2]):
    print(f'Question {i[1]}, Answer {m[1]}')

Hope this helps :)

\$\endgroup\$
2
\$\begingroup\$

It's not strictly necessary to build up lists. It is possible to implement a "streamed" version that runs a generator over lines of the open file.

Also, csv is of minimal help here: there are no special text-field delimiters like quotes, and there is only one field you care about - and there are no headers - so you might as well just do the trivial parsing yourself.

Suggested

from typing import Iterator


def parse(filename: str = 'answers.csv') -> Iterator:
    with open(filename, 'r') as f:
        while True:
            line = next(f, None)
            if line is None:
                break
            _, question = line.split(';', 1)
            line = next(f)
            _, answer = line.split(';', 1)
            yield question.rstrip(), answer.rstrip()


for question, answer in parse():
    print('?', question, '\n    >', answer)

Output

? (same.) 
    > Type...
?  (who are you?) 
    >  I am a talker whom the world has not yet seen. And you?
?  (leave.) 
    > Connect to the Internet - I'll leave...
?  (I would like to.) 
    > He would like to! And you're a no-brainer guy!
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.