简体   繁体   中英

With python, how to split string in each row of a csv file

This is how the csv file looks like

I have this banking dataset with all the variable names and item staying in the same cells of the column A. How do I separate them properly by ";", and place them in each column of the csv file following column A, with Python?

For example, all the variable names are stored in A1 :

age;"job";"marital";"education";"default";"housing";"loan";"contact";"month";"day_of_week";"duration";"campaign";"pdays";"previous";"poutcome";"emp.var.rate";"cons.price.idx";"cons.conf.idx";"euribor3m";"nr.employed";"y"

and one of the data in B1: 56;"housemaid";"married";"basic.4y";"no";"no";"no";"telephone";"may";"mon";261;1;999;0;"nonexistent";1.1;93.994;-36.4;4.857;5191;"no"

same with the data in A2, A3, A4......

instead I would like to figure out a way to separate all of them by ";" and place them in separated cells B1, C1, D1..... so they look like:

____A______B_____C______
1| Age_ |____job____| marital_|.....
2|__56_ | housemaid_|_married |..... ...... ......(I hope to do the same for all the rows)

I want to modify the file with Python, so with read.csv from pandas I can read/analyze the data with gridlines. I think I did something similar before with R.

First of all you should try to do it yourself first and then ask a question with a code sample.

Second, please accept answers that solve your question. (As I see the previous one wasn't accepted)

Thirdly, here is my shot at the code.

For example if you have your data set (I simplified mine, but its along the lines of your data):

"cat";"dog";"moose"
"moose";"cat";"dog"

And here is the code:

import csv

csv_rows = []

with open('animals.csv', 'rb') as csvfile:
    orig_csv = csv.reader(csvfile, delimiter=';')
    for row in orig_csv:
        csv_rows.append(row)

with open("animals_1.csv", "wb") as csvfile:
    w = csv.writer(csvfile, delimiter=",")
    for row in csv_rows:
        w.writerow(row)

Make sure you are using the correct delimiter for reading/writing the CSV! As the data set that you have would look fine on my PC.

Edited: changed the sample code a bit.

based on my understanding, the raw format of your data is like:

[root@ES01 ~]# cat /tmp/test.txt 
c1;c2;c3;c4;c5
v1;v2;v3;v4;v5

You want to change to

c1,c2,c3,c4,c5
v1,v2,v3,v4,v5

I think you can

f=open('/tmp/test.txt')
for line in f.readlines():
    print line.replace(';',',')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM