3

I'm trying to delete white spaces and enter's from a list, which i need to import as coordinates. However this does not seem to work. giving the following error:

AttributeError: 'list' object has no attribute 'strip'

Currently i'm still looking into the removal of the spaces (first these have to be deleted, then the enter's will follow).

Does anyone has any suggestions why this does not work?

the code is as follow:

# Open a file
Bronbestand = open("D:\\Documents\\SkyDrive\\afstuderen\\99 EEM - Abaqus 6.11.2\\scripting\\testuitlezen4.txt", "r")
headerLine = Bronbestand.readline()
valueList = headerLine.split(",")
#valueList = valueList.replace(" ","")

xValueIndex = valueList.index("x")
yValueIndex = valueList.index("y")
#xValueIndex = xValueIndex.replace(" ","")
#yValueIndex = yValueIndex.replace(" ","")

coordList = []

for line in Bronbestand.readlines():
    segmentedLine = line.split(",")
    coordList.append([segmentedLine[xValueIndex], segmentedLine[yValueIndex]])

coordList2 = [x.strip(' ') for x in coordList]

print coordList2

Where the "Bronbestand" is the following:

id,x,y,
      1,  -1.24344945,   4.84291601
      2,  -2.40876842,   4.38153362
      3,  -3.42273545,    3.6448431
      4,  -4.22163963,   2.67913389
      5,   -4.7552824,   1.54508495
      6,  -4.99013376, -0.313952595
      7,   -4.7552824,  -1.54508495
      8,  -4.22163963,  -2.67913389
      9,  -3.42273545,   -3.6448431

Thank you all in advance for the help!

4

6 に答える 6

4

あなたの問題はここにあるようです。このappend()メソッドは、単一の項目をリストに追加します。リストにリストを追加すると、リストのリストが得られます。

coordList.append([segmentedLine[xValueIndex], segmentedLine[yValueIndex]])

これを修正するには 2 つの方法があります。

# Append separately
coordList.append(segmentedLine[xValueIndex])
coordList.append(segmentedLine[yValueIndex])

# Use extend()
coordList.extend([segmentedLine[xValueIndex], segmentedLine[yValueIndex]])

または、リストのリストが必要な場合は、2 レベルの深さで繰り返す必要があります。

coordList2 = [[x.strip(' ') for x in y] for y in coordList]
于 2013-01-10T16:24:28.887 に答える
1
import csv
buff = csv.DictReader(Bronbestand)

result = []

for item in buff:
    result.append(dict([(key, item[key].strip()]) for key in item if key])) # {'y': '-3.6448431', 'x': '-3.42273545', 'id': '9'}

あなたのデータは有効です カンマ区切り値 (CSV) ネイティブの python csv パーサーを使用してみてください。

于 2013-01-10T16:29:55.513 に答える
0

coordList2要素リストのリストです。

そのようにしたい場合は、次のようにするstrip必要があります。

coordList2 = [[x[0].strip(' '), x[1].strip(' ')] for x in coordList]
于 2013-01-10T16:23:43.243 に答える
0

coordListここでは 2 レベルの深さなので、次のようにする必要があります。

coordList2 = [[a.strip(' '), b.strip(' ')] for a, b in coordList]

aしかし、よりわかりやすい名前bが良いかもしれません!

于 2013-01-10T16:24:22.013 に答える
0
for line in Bronbestand.readlines():
    segmentedLine = line.split(",")
    coordList.append(segmentedLine[xValueIndex])
    coordList.append(segmentedLine[yValueIndex])

以前は、余分なペアcoordListがあるため、文字列ではなく にリストを追加していました。[]

これはx、リスト内包表記 ( coordList2 = [x.strip(' ') for x in coordList]) がメソッドを持たないリストであることを意味しますstrip()

于 2013-01-10T16:24:30.017 に答える
0

にリストを追加していますcoordLis

coordList.append([segmentedLine[xValueIndex], segmentedLine[yValueIndex]])

より良い使用extend()。例えば:

In [84]: lis=[1,2,3]

In [85]: lis.append([4,5])

In [86]: lis
Out[86]: [1, 2, 3, [4, 5]]  # list object is appended

In [87]: lis.extend([6,7])     #use extend()

In [88]: lis
Out[88]: [1, 2, 3, [4, 5], 6, 7]     #6 and 7 are appended as normal elements
于 2013-01-10T16:25:59.273 に答える