-1

I'm doing some measurements in the lab and want to transform them into some nice Python plots. The problem is the way the software exports CSV files, as I can't find a way to properly read the numbers. It looks like this:

-10;-0,0000026  
-8;-0,00000139  
-6;-0,000000546   
-4;-0,000000112  
-2;-5,11E-09  
0,0000048;6,21E-09  
2;0,000000318  
4;0,00000304  
6;0,0000129  
8;0,0000724  
10;0,000268  

Separation by ; is fine, but I need every , to be .. Ideally I would like Python to be able to read numbers such as 6.21E-09 as well, but I should be able to fix that in excel...

My main issue: Change every , to . so Python can read them as a float.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
user12212883
  • 1
  • 1
  • 1

3 Answers3

0

The simplest way would be for you to convert them to string and then use the .replace() method to pretty much do anything. For i.e.

txt = "0,0000048;6,21E-09"
txt = txt.replace(';', '.')

You could also read the CSV file (I don't know how you are reading the file) but depending on the library, you could change the 'delimiter' (to : for example). CSV is Comma-separated values and as the name implies, it separates columns by means of '.

0

You can do whatever you want in Python, for example:

import csv

with open('path_to_csv_file', 'r') as csv_file:
    data = list(csv.reader(csv_file, delimiter=';'))

data = [(int(raw_row[0]), float(raw_row[1].replace(',', '.'))) for row in data]

with open('path_to_csv_file', 'w') as csv_file:
    writer = csv.writer(csv_file, delimiter=';')
    writer.writerows(data)
Adirio
  • 5,040
  • 1
  • 14
  • 26
  • I edited to make it a one-liner, using int for the first column to prevent the 2.0 notation and float with the replacement of `,` by `.` for the second column. – Adirio Oct 19 '20 at 10:25
  • Thank you very much, this helped! My problem is solved. – user12212883 Oct 21 '20 at 12:29
0

Can you consider a regex to match the ',' all in the text, then loop the match results in a process that takes ',' to '.'.

Allen
  • 88
  • 6