2

I hope somebody might help me with this. Please help me! :)

Data from csv-file:

Region      Municipio   laboratorio clinica Sospechosos Total casos
AMAZONAS    AMAZONAS    0         0          6      6
AMAZONAS    EL ENCANTO  0         0          0      0
ANTIOQUIA   ABEJORRAL   0         0      2      2
ANTIOQUIA   ABRIAQUI    0         0          0      0
ARAUCA  ARAUCA          6         13     0      19
ARAUCA  DESCONOCIDO 0          0     0       0
ARAUCA  ARAUQUITA   1         12     0      13
ARAUCA  TAME            2          1     0       3
ATLANTICO   ATLANTICO   0          0     4       4
ATLANTICO   BARANOA         1          9     0      10
AMAZONAS    PUERTO          0          0     0       0
AMAZONAS    TARAPACA    0          0     0       0
BOLIVAR ARENAL          0          0     29     29
BOLIVAR ARJONA          3          8     0      11

Task 1)

I need to create a function - extractDataFromFile(filename) that reads all the lines/rows in the csv-file.

Task 2)

The next function getRegionData(), must extract data (laboratorio, clinica, Sospechosos, Total casos) from each region using the previous function.

I have worked on this for a week now and I'm pretty lost. :( Hope you guys can help me ;)

3
  • What did you try so far? Please specify that in your question Commented Feb 29, 2016 at 17:22
  • first problem that is not a valid csv file Commented Feb 29, 2016 at 17:37
  • You should learn how to use "python i/o" to read and write documents; and learn how to use "pandas" to organize and display your data in DataFrames. Commented Feb 29, 2016 at 17:45

1 Answer 1

1
with open(filename) as f:
    lines = f.readlines()
    headers = lines[0].split("  ")
    for line in lines[1:]:
        data = [s.strip() for s in line.split("  ") if s]

data contains the info and headers the headers

also you can use the pandas and csv modules to do this

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

2 Comments

this answer would work if the file was formatted correctly but the header has too many splits (Sospechosos Total casos) will be 3 elements, and the data line 2 has (AMAZONAS EL ENCANTO) which will throw off the indexes - this is why i made the comment about it not being a valid csv - and simple split is problematic
@gkusner you are right but spliting on 2 spaces should be ok if their is no counter example

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.