I
I
Isaacer2021-06-09 08:49:23
Python
Isaacer, 2021-06-09 08:49:23

How to correctly copy text from one csv file to another?

The essence of the problem: my code (presented below) copies the columns from one "ishod" file to the "answer" file. All of them have ".csv" extension. But the problem is that they all end up in 2 columns and huge rows, even though the original file has many columns, not two. That is to say, the code for some reason turns the pillars over and turns them into rows. How to fix it?

file = open("ishod.csv","rb")
my_file = open("otvet.csv", "w+")

line_count = 0
for line in file:
    if line != "\n":

        print(line)
        my_file.write(str(line))
        line_count += 1

ishod.close()
otvet.close()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2021-06-09
@SoreMix

Because the file was opened to read bytes, it glues the line into one

file = open("ishod.csv","r")
my_file = open("otvet.csv", "w")

line_count = 0
for line in file:
    if line != "\n":

        print(line)
        my_file.write(line)
        line_count += 1

ishod.close()
otvet.close()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question