Skip to content Skip to sidebar Skip to footer

How To Split String Into Column

I got a csv file with some data, and I want to split this data. My column one contains a title, my column 2 contains some dates, and my column 3 contains some text linked to th

Solution 1:

If I understand you right, you have a csv file which looks like this:

title1,date1,text1
title2,date2,text2
title3,date3,text3

If you want to split the rows, simply use the .split()-function.

In this case, it'd look similar to this:

columns = row.split(',')

The .split()-function returns an array full of strings, therefore, if you want to access column 1, you have to say columns[0].

If it's the first row, columns[0] would give you title1 with my data above.


Another example:

filename = 'data.txt'lines = [line.strip() for line inopen(filename)]
for row inlines:
    columns = row.split(',')
    print' '.join(columns)

Using this code, gives you the following output:

title1 date1 text1
title2 date2 text2
title3 date3 text3

Post a Comment for "How To Split String Into Column"