English 中文(简体)
Python - 书写时使用多特性限制器
原标题:Python Pandas - use Multiple Character Delimiter when writing to_csv

看来,花板只能让单一特性的限定者/分离者。

是否允许使用像、“:”或“%”这样的特征?

我尝试:

df.to_csv(local_file,  sep =  :: , header=None, index=False)

加入:

TypeError: "delimiter" must be a 1-character string
问题回答

想到这一行文:a:b:c系指标准CSV工具:a,一栏空栏,b,一栏空栏,及/code>。 即便是在引证或教唆的更复杂的情况下:abc:def”:2 系指abc:def, 空栏和2。

因此,你们必须做的是在每个栏目之间增加一个空栏,然后使用<条码>:作为限定词,产出几乎是你想要的。

我说“几乎”是因为Pandas将引证或逃脱单一殖民地。 视辩证方案而定,你使用的手段以及你试图与外界互动的工具,可能不会成为问题。 没有必要的引文通常不成问题(除非你要求QUOTE_ALL,因为你的栏目将用<代码>分离:“,因此希望你无需选择方言”,但没有必要的越狱可能(例如,你可能最终用每条<>:,在一条形形形形形形形形形形形形形形形形形形色色色码中)。 因此,你们必须仔细考虑这些选择。 但是,它为基本“根据需要引用,大多数是标准的其他选择”环境付出了代价。

目前,我被困在老式的安达。 我的任务是阅读“......”限定语,清除个人识别信息,并将结果写上新的档案。 我需要这样的结果,才能有同样的双领。

我的首选解决办法是,像这样,转换为消费和挽救:

df = pandas.read_csv("patient_patient-final.txt", sep="__", engine="python")

# remove personal identifying info from dataframe

massaged = df.drop([ paternal_last ,  maternal_last ,  first ,  middle ,  suffix ,  prefix ,  street1 ,  street2 ,  phone1 ,  phone2 ,  email ,  emergencyfullname ,  emergencyphone ,  emergencyemail ,  curp ,  oldid ], axis=1)
np_data = massaged.to_numpy()
np.savetxt("patient_massaged.txt", np_data, fmt="%s", delimiter="__")

然而,在Pandas I的版本中,到_numpy()没有支持。

因此,我的定点是用“}”制成一个星体,以“}”作为限定语,但以变数计者除外,代之以直,自行撰写:

df = pandas.read_csv("patient_patient-final.txt", sep="__", engine="python")

# remove personal identifying info from dataframe

massaged = df.drop([ paternal_last ,  maternal_last ,  first ,  middle ,  suffix ,  prefix ,  street1 ,  street2 ,  phone1 ,  phone2 ,  email ,  emergencyfullname ,  emergencyphone ,  emergencyemail ,  curp ,  oldid ], axis=1)

x = massaged.to_csv(sep="}", header=False, index=False)
x = x.replace("}", "__")

f=open("patient_massaged.txt", "w")
f.write(x)
f.close()




相关问题
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 ]="...

热门标签