English 中文(简体)
将空间划界档案改为CSV
原标题:converting a space delimited file to a CSV
  • 时间:2012-01-14 00:11:10
  •  标签:
  • python

我有一份载有表格数据的文本文件。 我需要做的是,把撰写任务自动化到一个新文本档案中,该文本被压缩,而不是空间划界,从现有数据中提取几栏,重新排列各栏。

这是原始数据头4行的缩略语:

Number of rows: 8542
 Algorithm  |Date   |Time   |Longitude  |Latitude   |Country    
 1  2000-01-03  215926.688  -0.262  35.813  Algeria 
 1  2000-01-03  215926.828  -0.284  35.817  Algeria

Here is what I want in the end:

Longitude,Latitude,Country,Date,Time
-0.262,35.813,Algeria,2000-01-03,215926.688

如何处理这一问题?

最佳回答

我对档案持怀疑态度的是用表格而不是空子分开的。

如果是的话,你可以尝试:

input_file = open( some_tab_separated_file.txt ,  r )
output_file = open( some_tab_separated_file.csv ,  w )
input_file.readline() # skip first line 
for line in input_file:
    (a, date, time, lon, lat, country) = line.strip().split( 	 )
    output_file.write( , .join([lon, lat, country, date, time]) +  
 )
input_file.close()
output_file.close()

该法典未经测试,任何遗嘱都留给你。

问题回答

页: 1 的模块和一个阅读器,读到您的数据,并使用同一模块的作者(有星号)。

事实上,。 模块文件使用<代码>delimiter=。

您可使用<代码>DictReader/DictWriter,并具体说明其构件中各栏的顺序(fieldnames list:如果你想重订顺序,对读者/作者的不同之处)按您的意愿制作这些条目。

(在生产产出时,你也许需要精干/解头两行。)

http://www.un.org。

以下是处理多语国名的例子:

import cStringIO
import csv

f = cStringIO.StringIO("""A B C
1 2 Costa Rica
3 4 Democratic Republic of the Congo
""")

r = csv.DictReader(f, delimiter=   , restkey= rest )
for row in r:
    if row.get( rest ):
        row[ C ] += " %s" % (" ".join(row[ rest ]))
    print  A: %s, B: %s, C: %s  % (row[ A ], row[ B ], row[ C ])

使用<代码>restkey=,并按这一价值计算压缩字句编号,该表是左边的列表(见restkey=休息)。 印本:

A: 1, B: 2, C: Costa Rica
A: 3, B: 4, C: Democratic Republic of the Congo

str.split(), 否则任何论点都将按白空间的长短分开。 <编码>处理器.itemgetter()有多种论点,并将重述。

我猜测这一重要想法是,你必须作为划界者@Paulo Scardine。

我只想补充一点,安达是处理一栏数据的非常好的图书馆。

>>> src =  path/to/file 
>>> dest =  path/to/dest_csv 
>>> column_names = [ names ,  of ,  columns ]

>>> df = pd.read_csv(src, delimiter= 	 , names=column_names)

# Do something in pandas if you need to

>>> df.to_csv(dest, index=False, sep =  ; )




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

热门标签