On 25/06/18 20:35, Giulia Marcoux wrote:
> Hello,
>
> I am a student learning python, and i am running into some difficulties. I
> am tryign to read a csv file that has different delimiters for different
> rows: Example:
>
> Format:V1.1
> Model: R
> Step Size: 10mm
> Distance: 10cm
> Gain: 1000

> X,Y
> 1,3
> 2,5
> 6,5
> 5,7
>

Hi Giulia,

When dealing with unformatted data files, a first quick approach
is to read in using standard python routines to read the file
and then proceed via looping the lines of the file. Here is a 
very crude code to deal with your sample
(in what follows >>>> represents indentations):

# Say you have named your data file "thefile.dat"

thefilename = "./thefile.dat" # Assign the filename to a variable

# read the whole file
with open(thefilename, 'r') as theFile:
>>> contentfile = theFile.read()

# print(contentfile) # peruse the file content. Find patterns to extract data

# split the file in lines according to the newline character "\n"
templines = []
for line in contentfile.splitlines():
>>>> templines.append(line.strip())

#Following the pattern extract the data
thelines = []
for line in templines:
>>>if ':' in line:
>>>>>>temp = line.split(":")
>>>>>>for i in range(len(temp)):
>>>>>>>>try:
>>>>>>>>>>>>temp[i] = float(temp[i])
>>>>>>>>except:
>>>>>>>>>>>>temp[i] = temp[i]
>>>>>>print(temp)
>>>>>>thelines.append(temp)
>>>elif ',' in line:
>>>>>>temp = line.split(",")
>>>>>>for i in range(len(temp)):
>>>>>>>>try:
>>>>>>>>>>>>temp[i] = float(temp[i])
>>>>>>>>except:
>>>>>>>>>>>>>>>temp[i] = temp[i]
>>>>>>print(temp)
>>>>>>thelines.append(temp)
>>>else: # not necessary
>>>>>>>>pass
# print(thelines) # check a few lines

# Improve the code to deal with large files (big data stuff)
# your turn ...

# make a pandas data frame from the data
import pandas as pd
import numpy as np

mypdframe = pd.DataFrame(np.array(thelines))

print(mypdframe)

Hope this set your learning of python not too stressful,
so you could appreciate its power.

Sergio

Check out the free first draft of the book:
    Prealgebra via Python Programming

        https://www.researchgate.net/publication/325473565
Companion web site:
      https://github.com/rojassergio/Prealgebra-via-Python-Programming





_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to