0

I have a csv file with the following format

21/10/2017 0:00;123,85;88,8

and i use the command to parse it

with open('parseme.csv') as csv_file:
reader = csv.reader(csv_file)

So I get the values as

['21/10/2017 0:00;123', '85;88', '8']

The problem is I want the value after the date to be altogether because I cannot convert it to integer due to the quote characters in the middle before and after comma. How can I achieve to import it as it was in the CSV, like this:

['21/10/2017 0:00', '123,85', '88,8']
12
  • 1
    If you want it as a single string, why use csv at all? Commented Jan 24, 2018 at 12:52
  • 1
    Do you understand that the quote characters in the output are not actually part of your output? That's just how Python prints out a list of strings. Commented Jan 24, 2018 at 12:54
  • Fix the delimiter and the csv module will do the conversion for you. Which, I assume, is the reason you used that package in the first place Commented Jan 24, 2018 at 12:54
  • Did you mean ['21/10/2017 0:00', '123,85', '88,8']? Commented Jan 24, 2018 at 12:55
  • @MadPhysicist I don't think that's what OP wants but I also don't see why they would want is as ['21/10/2017 0:00;123,85;88,8'] They maybe meant: ['21/10/2017', '0:00;123,85;88,8'] Commented Jan 24, 2018 at 12:57

1 Answer 1

2

First, you need to get the csv fields by splitting on the actual delimiter you're using (which is a semicolon, not a comma):

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

The result of iterating on reader now will be a series of lists of strings, looking like the following:

['21/10/2017 0:00', '123,85', '88,8']

(Note: this is the python representation for strings, the ' characters are not actually part of the data)

Now, to get to the actual numbers, you need to turn those strings to values. The second and the third are more or less straightforward, but you need to take care of that comma. The value you have is using a locale in which decimal values are separated by a comma, python expects a dot. So, we can convert them as follows (Let line be one line from the reader):

second_number = float(l[1].replace(',','.'))
third_number = float(l[2].replace(',','.'))

For the date the thing is more complicated. Assuming you're just interested in the numbers in the date and not a full conversion to some datetime value, this is what you could do:

date, time = line[0].split(' ') #separate "21/10/2017" from "0:00"
day, month, year = [int(v) for v in date.split('/')]
hour, minute = [int(v) for v in time.split(':')]

I hope this is clear enough and matching what you need

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

16 Comments

This will split by ; meaning the output will be ['21/10/2017 0:00', '123,85', '88,8'] which is not what OP wants.
From the wording of the question, I believe what the OP wants is to parse the data in the csv, which this does. I do understand that the literal interpretation of the question is "how do I read a file line-by-line", but I believe this is not what the OP actually wants
I added the question mark but after that I get this is time: 0:00' this is the day: ['21 so I still get characters I don't want. I want to make 21 day an integer, and python interprets the [' also, so I am getting an error. I want to ommit [' from this and get only the 21
Could you clarify what you mean by "Added the question mark"?
@MadPhysicist Done :)
|

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.