读非均匀线ascii数据 - Python

时间:2017-07-24 16:08:30

标签: python pandas numpy ascii readlines

我试图读取非均匀线ascii数据,例如

 4  0.0790926412 -0.199457773  0.325952223  0.924105917  48915.3072 -2086.17061
  73540.4807 10
 4  0.0245689377 -0.805261448 -0.152373497  0.573006386 -39801.696  49084.2418
  16665.3857 10
 4  0.0427767979 -0.0185129676 -0.143135691 -0.989529911  38770.6518
 -70784.7024  32640.6307 10
 4  0.0262684678  0.137741 -0.820259709 -0.555158921  25293.3918 -51148.4003
 -126522.859 10
 4  0.145932295  0.466618154 -0.00805648931 -0.88442218  90951.8483  19221.4234
 -40205.3438 10
 4  0.0907820906  0.584060054 -0.671576188  0.455915866 -78193.2124 -31269.5848
  47260.338 10
 4  0.0794897928  0.654042761  0.537625452  0.532153117  24643.9195  39614.3788
  97184.4856 10
 4  0.0896920622 -0.517384933 -0.609729743 -0.600451889 -17455.9074 -17601.0439
 -13991.5163 10
 4  0.0295554749 -0.53757783 -0.3710939  0.757165368  20106.124 -171013.738
 -14052.1145 10
 4  0.0189505245 -0.773354757 -0.0747623556 -0.629549847 -71468.2726
 -53145.1259  36948.4058 10

问题是我需要将每两行读成一行。我正在尝试使用pandas.read_csvnumpy.genfromtxt,但他们会阅读并分成独立的行。我尝试合并每两行没有成功,因为,你怎么看,有时我有一个分为7和2列的行,6和3列的somentimes。总共有9列要读。

3 个答案:

答案 0 :(得分:1)

这样的事情应该有效。

将数据放入字符串或文档中,然后使用python进行操作。然后,当您拥有您想要的数据时,就可以使用pandas。

string1 = '''4  0.0790926412 -0.199457773  0.325952223  0.924105917  48915.3072 -2086.17061
  73540.4807 10
 4  0.0245689377 -0.805261448 -0.152373497  0.573006386 -39801.696  49084.2418
  16665.3857 10
 4  0.0427767979 -0.0185129676 -0.143135691 -0.989529911  38770.6518
 -70784.7024  32640.6307 10
 4  0.0262684678  0.137741 -0.820259709 -0.555158921  25293.3918 -51148.4003
 -126522.859 10
 4  0.145932295  0.466618154 -0.00805648931 -0.88442218  90951.8483  19221.4234
 -40205.3438 10
 4  0.0907820906  0.584060054 -0.671576188  0.455915866 -78193.2124 -31269.5848
  47260.338 10
 4  0.0794897928  0.654042761  0.537625452  0.532153117  24643.9195  39614.3788
  97184.4856 10
 4  0.0896920622 -0.517384933 -0.609729743 -0.600451889 -17455.9074 -17601.0439
 -13991.5163 10
 4  0.0295554749 -0.53757783 -0.3710939  0.757165368  20106.124 -171013.738
 -14052.1145 10
 4  0.0189505245 -0.773354757 -0.0747623556 -0.629549847 -71468.2726
 -53145.1259  36948.4058 10'''

splitted = string1.splitlines()
result = ""
for index,item in enumerate(splitted):
  if index % 2 != 0:
    result += item+ "\n"
  else:
       result += item 
print(result)

4  0.0790926412 -0.199457773  0.325952223  0.924105917  48915.3072 -2086.17061  73540.4807 10
 4  0.0245689377 -0.805261448 -0.152373497  0.573006386 -39801.696  49084.2418  16665.3857 10
 4  0.0427767979 -0.0185129676 -0.143135691 -0.989529911  38770.6518 -70784.7024  32640.6307 10
 4  0.0262684678  0.137741 -0.820259709 -0.555158921  25293.3918 -51148.4003 -126522.859 10
 4  0.145932295  0.466618154 -0.00805648931 -0.88442218  90951.8483  19221.4234 -40205.3438 10
 4  0.0907820906  0.584060054 -0.671576188  0.455915866 -78193.2124 -31269.5848  47260.338 10
 4  0.0794897928  0.654042761  0.537625452  0.532153117  24643.9195  39614.3788  97184.4856 10
 4  0.0896920622 -0.517384933 -0.609729743 -0.600451889 -17455.9074 -17601.0439 -13991.5163 10
 4  0.0295554749 -0.53757783 -0.3710939  0.757165368  20106.124 -171013.738 -14052.1145 10

或者如果您从文件中读取它:

data = open('/path/original.txt', 'r')
string1 = data.read()
splitted = string1.splitlines()
result = ""
for index,item in enumerate(splitted):
   if index % 2 != 0:
     result += item+ "\n"
   else:
     result += item
new_data = open('/path/new_data.txt','w')
new_data.write(result)

答案 1 :(得分:1)

如果我,我想以这种方式这样做:

import re
with open('data.txt') as f:
    s = f.read().strip()
L = [float(i) for i in re.split(r'\s+', s)]
LL = [L[i:i+9] for i in range(0, len(L), 9)]
print(LL)

[[4.0,0.0790926412,-0.199457773,0.325952223,0.924105917,48915.3072,-2086.17061,73540.4807,10.0],[4.0,0.0245689377,-0.805261448,-0.152373497,0.573006386,-39801.696,49084.2418,16665.3857,10.0],[ 4.0,0.0427767979,-0.0185129676,-0.143135691,-0.989529911,38770.6518,-70784.7024,32640.6307,10.0],[4.0,0.0262684678,0.137741,-0.820259709,-0.555158921,2529.3918,-51148.4003,-126522.859,10.0],[4.0, 0.145932295,0.466618154,-0.00805648931,-0.88442218,90951.8483,19221.4234,-40205.3438,10.0],[4.0,0.0907820906,0.584060054,-0.671576188,0.455915866,-78193.2124,-31269.5848,47260.338,10.0],[4.0,0.0794897928,0.654042761, 0.537625452,0.532153117,24643.9195,39614.3788,97184.4856,10.0],[4.0,0.0896920622,-0.517384933,-0.609729743,-0.600451889,-17455.9074,-17601.0439,-13991.5163,10.0],[4.0,0.0295554749,-0.53757783,-0.3710939, 0.757165368,20106.124,-171013.738,-14052.1145,10.0],[4.0,0.0189505245,-0.773354757,-0.0747623556,-0.62954 9847,-71468.2726,-53145.1259,36948.4058,10.0]]

答案 2 :(得分:1)

或者像这样,因为你知道每个案例有两行。

每次循环读取两行输入。当第一行为空时,这意味着输入文件中没有更多行可用。每次读取一对行时,它们首先丢弃从第一行开始的行。

Pandas可以读取使用空格代替逗号的'csv'文件。

>>> import pandas as pd
>>> with open('temp.txt') as input, open('temp.csv', 'w') as the_csv:
...     while True:
...         first = input.readline()
...         if not first:
...             break
...         second = input.readline()
...         r = the_csv.write(first.strip()+second)
... 
>>> df = pd.read_csv('temp.csv', sep='\s+')
>>> df
   4  0.0790926412  -0.199457773  0.325952223  0.924105917  48915.3072  \
0  4      0.024569     -0.805261    -0.152373     0.573006 -39801.6960   
1  4      0.042777     -0.018513    -0.143136    -0.989530  38770.6518   
2  4      0.026268      0.137741    -0.820260    -0.555159  25293.3918   
3  4      0.145932      0.466618    -0.008056    -0.884422  90951.8483   
4  4      0.090782      0.584060    -0.671576     0.455916 -78193.2124   
5  4      0.079490      0.654043     0.537625     0.532153  24643.9195   
6  4      0.089692     -0.517385    -0.609730    -0.600452 -17455.9074   
7  4      0.029555     -0.537578    -0.371094     0.757165  20106.1240   
8  4      0.018951     -0.773355    -0.074762    -0.629550 -71468.2726   

   -2086.17061   73540.4807  10  
0   49084.2418   16665.3857  10  
1  -70784.7024   32640.6307  10  
2  -51148.4003 -126522.8590  10  
3   19221.4234  -40205.3438  10  
4  -31269.5848   47260.3380  10  
5   39614.3788   97184.4856  10  
6  -17601.0439  -13991.5163  10  
7 -171013.7380  -14052.1145  10  
8  -53145.1259   36948.4058  10