English 中文(简体)
如何通过逗号分隔符和双引号将文本转换为CSV?
原标题:How to convert text into a CSV by comma delimiter and double quotes?

这是我当前的字符串值:

"""
    |name, model, os
    |A,"I PAD (10.0"", 2020, Wi-Fi)",OS_A
"""

我希望输出如下,并最终保存为csv:

name model os
A I PAD (10.0"", 2020, Wi-Fi) OS

我被绊倒了,因为在模型字段中,字符串中有逗号和双引号。我目前的想法是正则表达式任何有问题的文本,但有其他解决方案吗?

问题回答

https://onlinegdb.com/cslea1uYz

import csv

string = 
"""
    |name, model, os
    |A,"I PAD (10.0"", 2020, Wi-Fi)",OS_A
    |B,"I PAD (10.0"", 2020, Wi-Fi)",OS_B
    |C,"I PAD (10.0"", 2020, Wi-Fi)",OS_C
    |D,"I PAD (10.0"", 2020, Wi-Fi)",OS_D
"""

reader = csv.reader(string.splitlines(), quotechar= " )
with open( output.csv ,  w , newline=None) as file:
    writer = csv.writer(file, quotechar= " )
    for row in reader:
        if not row: continue
        row = (i.strip(  | ) for i in row)
        writer.writerow(row)

假设您的输入数据格式一致,我们可以使用创造性的可迭代解压缩来容忍中间列中的。只要外部列不包含逗号,我们就可以使用pandas.to_csv()来编写csv

import pandas as pd

input_string =    
    |name, model, os
    |A,"I PAD (10.0"", 2020, Wi-Fi)",OS_A
    |B,"I PAD (10.0"", 2020, Wi-Fi)",OS_B
   

lines = [line.strip().strip( | ).split( , ) for line in input_string.strip().split( 
 )]
(name,*model,os) = lines[0] 
header= (name, , .join(model),os)

lines= [(name, , .join(model).strip( " ),os) for (name,*model,os) in lines[1:]]
pd.DataFrame(lines,columns=header).to_csv( data.csv ,index=False)

输出数据帧

    name    model   os
0   A   I PAD (10.0"", 2020, Wi-Fi) OS_A
1   B   I PAD (10.0"", 2020, Wi-Fi) OS_B

csv.read_csvquotechar的结合看起来非常强大,代码读起来也很好:

import csv

string = 
"""
    |name, model, os
    |A,"I PAD (10.0"", 2020, Wi-Fi)",OS_A
    |B,"I PAD (10.0"", 2020, Wi-Fi)",OS_B
    |C,"I PAD (10.0"", 2020, Wi-Fi)",OS_C
    |D,"I PAD (10.0"", 2020, Wi-Fi)",OS_D
"""

reader = csv.reader([line.lstrip(  |	 ) for line in string.splitlines()], quotechar= " )
header = None
while not header:
    header = next(reader)
pd.DataFrame(reader, columns=header).to_csv( name.csv ,index=False)

但这确实与outut“”字符有关:

    name    model   os
0   A   I PAD (10.0", 2020, Wi-Fi)  OS_A
1   B   I PAD (10.0", 2020, Wi-Fi)  OS_B
2   C   I PAD (10.0", 2020, Wi-Fi)  OS_C
3   D   I PAD (10.0", 2020, Wi-Fi)  OS_D

如果我理解你的问题,这应该能让你找到你想要的。

import pandas as pd
from io import StringIO
import re

string =    
    |name, model, os
    |A,"I PAD (10.0"", 2020, Wi-Fi)",OS_A
    |B,"I PAD (10.0"", 2020, Wi-Fi)",OS_B
    |C,"I            PAD (10.0"", 2020, Wi-Fi)", OS_C
   

string = re.sub("[|]", "", string)
string = re.sub(" ", "", string)

df = pd.read_csv(StringIO(string))
print(df)

下面是输出:

  name                   model    os
0    A  IPAD(10.0",2020,Wi-Fi)  OS_A
1    B  IPAD(10.0",2020,Wi-Fi)  OS_B
2    C  IPAD(10.0",2020,Wi-Fi)  OS_C

这假设了与输入的一致性,所以如果所有输入字符串都有点不同,那么您可能需要添加一些内容。

如果您喜欢保留一个空格,请使用string=re.sub(“+”,“”,string)

这是PySpark的解决方案,我用Spark 3.4和Python 3.11执行这个例子。

创建一个包含以下内容的输入.csv文件。

名称、型号、操作系统

A、 “I PAD(10.0英寸,2020,Wi-Fi)”,操作系统_A

PySpark代码:

import pyspark
from pyspark.sql import SparkSession
from pyspark.sql.functions import concat,lit,substring

# Create SparkSession
spark=SparkSession.builder.getOrCreate()

file_df=spark.read.csv("input.csv",header=True,quote= " ,escape= " )

file_df.show(truncate=False)
#+----+--------------------------+----+
#|name| model                    | os |
#+----+--------------------------+----+
#|A   |I PAD (10.0", 2020, Wi-Fi)|OS_A|
#+----+--------------------------+----+




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签