Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 the dates.

I want to transform that in something like :

title 0 Date 0.1 text 0.1
title 0 Date 0.2 text 0.2
title 1 date 1.0 text 1.0
title 1 date 1.1 text 1.1

How can I do that?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
161 views
Welcome To Ask or Share your Answers For Others

1 Answer

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 in open(filename)]
for row in lines:
    columns = row.split(',')
    print ' '.join(columns)

Using this code, gives you the following output:

title1 date1 text1
title2 date2 text2
title3 date3 text3

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...