2

I was hoping someone could help me with this. I'm getting a file from a form in Django, this file is a csv and I'm trying to read it with Python's library csv. The problem here is that when I apply the function csv.reader and I turn that result into a list in order to print it, I find out that csv.reader is not splitting correctly my file. Here are some images to show the problem

This is my csv file: enter image description here

This my code: enter image description here

And this is the printed value of the variable file_readed: enter image description here

As you can see in the picture, it seems to be splitting my file character by character with some exceptions. I thank you for any help you can provide me.

1

4 Answers 4

2

If you are pulling from a web form, try getting the csv as a string, confirm in a print or debug tool that the result is correct, and then pass it to csv using StringIO.

from io import StringIO
import csv

csv_string = form.files['carga_cie10'].file_read().decode(encoding="ISO-88590-1")
csv_file = StringIO(csv_string)
reader = csv.reader(csvfile, delimiter=',', quotechar='"')
for row in reader:
    print(row)

Another thing you can try is changing the lineterminator argument to csv.reader(). It can default to \r\n but the web form might use some other value. Inspect the string you get from the web form to confirm.

Sign up to request clarification or add additional context in comments.

2 Comments

I misread your question. Can you try getting a string from the web form and see if that looks correct to you?
Thank you, brother, this worked perfectly for me and it's splitting everything correctly. You really saved my day!
0

that CSV does not seem right: you got some lines with more arguments than others.

The acronym of CSV being Comma Separated Values, you need to have the exact same arguments separated by commas for each line, or else it will mess it up.

I see in your lines you're maybe expecting to have 3 columns, instead you got lines with 2, or 4 arguments, and some of them have an opening " in one argument, comma, then closing " in the second argument

check if your script works with other CSVs maybe

2 Comments

I was reading that csv manually and using the function split, the problem were those columns with the " opening. I searched and found out that csv reader will split correctly those columns. As for the empty columns, I have worked with csv that had empty columns and it worked perfectly.
what about the overflowing ones?
0

Most likely you need to specify delimiter. Since you haven't explicitly told about the delimiter, I guess it's confused.

csv.reader(csvfile, delimiter=',')

However, since there are quotations with comma delimiter, you may need to alter the default delimiter on the CSV file's creation too for tab or something else.

2 Comments

I have already try specifying the delimiter and it didn't work.
I guess you need to alter delimiter on the file creation by changing default delimiter to semi-colon or tab. Then it'll be able to read. Because you have more commas than 2 on 3rd line of CSV.
0

The problem is here:

print(list(file_readed)) 

'list' is causing printing of every element within the csv as an individual unit.

Try this instead:

with open('carga_cie10') as f:
reader = csv.reader(f)
for row in reader:
    print(" ".join(row))

Edit:

import pandas as pd
file_readed = pd.read_csv(file_csv)
print(file_readed)

The output should look clean. Pandas is highly useful in situations where data needs to be read, manipulated, changed, etc.

2 Comments

The thing is that I do not have physcally the file, I'm getting directly from the form, so I haven't been able to open the file like that.
Editing to suggest using Pandas instead.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.