2017-07-24 77 views
0

我试图读取非统一行ascii数据,例如:读取非统一行ascii数据 - Python

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列可供阅读。

回答

1

像这样的东西应该工作。

把你的数据放在一个字符串或文档中,并用python进行处理。然后,当你有你想要的数据时,你可以使用熊猫。

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

谢谢,我只在代码中添加了以下内容。为了读取字符串,我使用了data = open('/ path/original.txt,'r'),然后string1 = data.read()。在运行所有代码之后,我需要保存字符串重新格式化,所以我写了一个新文件,如new_data = open('/ path/new_data.txt','w'),然后new_data.write(result)。之后,我用熊猫读它!也许你可以在你的答案中加入更详细的内容。再次感谢。 – nandhos

+1

干了!我刚添加了上一版中错过的引号 – nandhos

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, - [4.0,0.0262684678,0.1137741,-0.820259709,-0.555158921,25293.3918,-51148.4003,-126522.859,10.0],[4.0,0.1145932295,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.629549847,-71468.2726,-53145.1259,36948.4058,10.0]]

1

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

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

熊猫可以读取使用空格代替逗号的'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