-4

How to reorganize CSV files with Python? Mine looks like this:

.......................<BR>
1987
martin
2012-01-16
1976
roger<BR>
1987
martin
2012-01-17
1979
michael<BR>
1969
maria
2012-01-26
1979
michael<BR>
......................................

These represent the birth year on top and the name underneath. In between is the date they established contact with each other. There are also some empty rows in between the blocks.

Each 5-row block represents two persons and that they have contacted other. So basically each 5-row block is two NODES and a LINK between them.

I would like to import this in Gephi, and thereby the CSV files need to be reorganized. Maybe as GEXF (Graph Exchange XML Format). Otherwise simply CSV like this would be OK:

................................<BR>
Source;Target;Label<BR>
1987 martin;1976 roger;"2012-01-16"<BR>
1987 martin;1979 michael;"2012-01-17"<BR>

And so on. How do I process the CSV to look like the above?

user4157124
  • 2,809
  • 13
  • 27
  • 42
user9200
  • 1
  • 2

2 Answers2

0

Python comes with a CSV module which, when set up with the correct delimiter, would parse the first file for you. If you wanted the output in the second format you have in your question, you could use the CSV module again (with a different delimiter of course) to create it.

If you want an XML-based format, Python comes with a number of different XML modules - see the list of markup modules in the documentation. There are also some add-on XML modules you can find through a web search.

Also, a simple Google search for Python GEXF finds a couple of modules for handling GEXF.

Now your basic research has been done for you, have a go at writing some code and come back with any specific questions you have.

Blair
  • 15,356
  • 7
  • 46
  • 56
0

Open your textfile in excel. Convert text -> column with space as a delimiter. Reorganize the columns with drag and drop. Save as CSV.

Hampus Brynolf
  • 1,296
  • 11
  • 20