如何在python中使用csv的分隔符

时间:2022-09-23 07:46:40

I'm having trouble with figuring out how to use the delimiter for csv.writer in Python. I have a csv file in which the strings separated by commas are in single cell and I need to have each word in each individual cell. For ex:

我在查找如何在Python中使用csv.writer的分隔符时遇到了麻烦。我有一个csv文件,其中用逗号分隔的字符串在单个单元格中,我需要在每个单独的单元格中包含每个单词。例如:

   100 , 2559   ,,Main, St,LEOMA,LEOMA,498,498, AK,AK
   140 , 425    ,,Main, St,LEOMA,LEOMA,498,498, AK,AK
   100 , 599    ,,Main, St,LEOMA,LEOMA,498,498, AK,AK

should be

应该

   100  2559        Main    St  LEOMA   LEOMA   498 498 AK  AK
   140  425     Main    St  LEOMA   LEOMA   498 498 AK  AK
   100  599     Main    St  LEOMA   LEOMA   498 498 AK  AK

(each word in individual cell).

(单个单元格中的每个单词)。

I tried:

我试过了:

import csv
workingdir = "C:\Mer\Ven\sample"
csvfile = workingdir+"\test3.csv"
f=open(csvfile,'wb')

csv.writer(f, delimiter =' ',quotechar =',',quoting=csv.QUOTE_MINIMAL)

2 个解决方案

#1


13  

Your code is blanking out your file:

您的代码正在删除您的文件:

import csv
workingdir = "C:\Mer\Ven\sample"
csvfile = workingdir+"\test3.csv"
f=open(csvfile,'wb') # opens file for writing (erases contents)
csv.writer(f, delimiter =' ',quotechar =',',quoting=csv.QUOTE_MINIMAL)

if you want to read the file in, you will need to use csv.reader and open the file for reading.

如果要读取文件,则需要使用csv.reader并打开文件进行读取。

import csv
workingdir = "C:\Mer\Ven\sample"
csvfile = workingdir+"\test3.csv"
f=open(csvfile,'rb') # opens file for reading
reader = csv.reader(f)
for line in reader:
    print line

If you want to write that back out to a new file with different delimiters, you can create a new file and specify those delimiters and write out each line (instead of printing the tuple).

如果要将其写回具有不同分隔符的新文件,则可以创建新文件并指定这些分隔符并写出每一行(而不是打印元组)。

#2


3  

ok, here is what i understood from your question. You are writing a csv file from python but when you are opening that file into some other application like excel or open office they are showing the complete row in one cell rather than each word in individual cell. I am right??

好的,这是我从你的问题中理解的。您正在从python编写csv文件,但是当您将该文件打开到其他应用程序(如excel或open office)时,它们会在一个单元格中显示完整的行,而不是单个单元格中的每个单词。我是对的??

if i am then please try this,

如果我那么请试试这个,

import csv

with open(r"C:\\test.csv", "wb") as csv_file:
    writer = csv.writer(csv_file, delimiter =",",quoting=csv.QUOTE_MINIMAL)
    writer.writerow(["a","b"])

you have to set the delimiter = ","

你必须设置delimiter =“,”

#1


13  

Your code is blanking out your file:

您的代码正在删除您的文件:

import csv
workingdir = "C:\Mer\Ven\sample"
csvfile = workingdir+"\test3.csv"
f=open(csvfile,'wb') # opens file for writing (erases contents)
csv.writer(f, delimiter =' ',quotechar =',',quoting=csv.QUOTE_MINIMAL)

if you want to read the file in, you will need to use csv.reader and open the file for reading.

如果要读取文件,则需要使用csv.reader并打开文件进行读取。

import csv
workingdir = "C:\Mer\Ven\sample"
csvfile = workingdir+"\test3.csv"
f=open(csvfile,'rb') # opens file for reading
reader = csv.reader(f)
for line in reader:
    print line

If you want to write that back out to a new file with different delimiters, you can create a new file and specify those delimiters and write out each line (instead of printing the tuple).

如果要将其写回具有不同分隔符的新文件,则可以创建新文件并指定这些分隔符并写出每一行(而不是打印元组)。

#2


3  

ok, here is what i understood from your question. You are writing a csv file from python but when you are opening that file into some other application like excel or open office they are showing the complete row in one cell rather than each word in individual cell. I am right??

好的,这是我从你的问题中理解的。您正在从python编写csv文件,但是当您将该文件打开到其他应用程序(如excel或open office)时,它们会在一个单元格中显示完整的行,而不是单个单元格中的每个单词。我是对的??

if i am then please try this,

如果我那么请试试这个,

import csv

with open(r"C:\\test.csv", "wb") as csv_file:
    writer = csv.writer(csv_file, delimiter =",",quoting=csv.QUOTE_MINIMAL)
    writer.writerow(["a","b"])

you have to set the delimiter = ","

你必须设置delimiter =“,”