How to split string into column

2024/11/17 23:27:50

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?

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
https://en.xdnf.cn/q/118739.html

Related Q&A

remove whitespaces with new line characters

I have a string that looks like that:"\n My name is John\n and I like to go.\n blahblahblah.\n \n\n ".Note - In this string example, there are 5 white-spaces after…

Python tkinter GUI freezing/crashing

from Tkinter import * import tkFileDialog import tkMessageBox import os import ttkimport serial import timeit import time################################################################################…

If/else in python list comprehension

I would like to return random word from file, based on passed argument. But if the argument doesnt match anythning I dont want to return anything. My method looks like:def word_from_score(self,score):p…

Conditional module importing in Python

Im just trying out Maya 2017 and seen that theyve gone over to PySide2, which is great but all of my tools have import PySide or from PySide import ... in them.The obvious solution would be to find/rep…

AttributeError: list object has no attribute lower : clustering

Im trying to do a clustering. Im doing with pandas and sklearn. import pandas import pprint import pandas as pd from sklearn.cluster import KMeans from sklearn.metrics import adjusted_rand_score from s…

I’m dealing with an error when I run server in Django

PS C:\Users\besho\OneDrive\Desktop\DjangoCrushcourse> python manage.py runserver C:\Users\besho\AppData\Local\Programs\Python\Python312\python.exe: cant open file C:\Users\besho\OneDrive\Desktop\Dja…

python threading with global variables

i encountered a problem when write python threading code, that i wrote some workers threading classes, they all import a global file like sharevar.py, i need a variable like regdevid to keep tracking t…

How to write nth value of list into csv file

i have the following list : sec_min=[37, 01, 37, 02, 37, 03, 37, 04, 37, 05,....]i want to store this list into CVS file in following format: 37,01 37,02 37,03 37,04 ... and so onthis is what i coded: …

Read R function output as columns

Im trying to come up with a way to solve this question I asked yesterday:rpy2 fails to import rgl R packageMy goal is to check if certain packages are installed inside R from within python.Following th…

How does .split() work? - Python

In the following examples, I am splitting an empty string by a space. However, in the first example I explicitly used a space and in the second example, I didnt. My understanding was that .split() and …