Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:删除空格和'\n';来自列表_Python_List - Fatal编程技术网

Python:删除空格和'\n';来自列表

Python:删除空格和'\n';来自列表,python,list,Python,List,我试图从列表中删除空格并输入,我需要将其作为坐标导入。然而,这似乎不起作用。给出以下错误: AttributeError: 'list' object has no attribute 'strip' 目前我仍在考虑删除空格(首先必须删除这些空格,然后输入) 有人有什么建议为什么这不起作用吗 代码如下: # Open a file Bronbestand = open("D:\\Documents\\SkyDrive\\afstuderen\\99 EEM - Abaqus 6.11.2\\s

我试图从列表中删除空格并输入,我需要将其作为坐标导入。然而,这似乎不起作用。给出以下错误:

AttributeError: 'list' object has no attribute 'strip'
目前我仍在考虑删除空格(首先必须删除这些空格,然后输入)

有人有什么建议为什么这不起作用吗

代码如下:

# 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
其中“最佳铜带”如下所示:

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

提前感谢大家的帮助

coordList
是由两个元素列表组成的列表

如果您希望这样,您的
条带
行应该是:

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

coordList
在这里有两层,因此您应该执行以下操作:

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

但是比
a
b
更多的描述性名称可能会更好

看来你的问题就在这里。
append()
方法将单个项目添加到列表中。如果将列表附加到列表,则会得到列表列表

coordList.append([segmentedLine[xValueIndex], segmentedLine[yValueIndex]])
有两种方法可以解决这个问题

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

# Use extend()
coordList.extend([segmentedLine[xValueIndex], segmentedLine[yValueIndex]])
或者,如果你想要一个列表列表,你需要迭代两个层次

coordList2 = [[x.strip(' ') for x in y] for y in coordList]
以前,由于额外的
[]
对,您将列表附加到
coordList
,而不是字符串


这意味着列表理解中的
x
coordList2=[x.strip(“”)代表coordList中的x]
)是一个列表,没有
strip()
方法。

您正在将列表附加到
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

您的数据是有效的逗号分隔值(CSV),请尝试使用本机python CSV解析器。

您正在将列表附加到
coordList
coordList。附加([segmentedLine[xValueIndex],segmentedLine[yValueIndex]])
,改为尝试
extend()
。抱歉,我不得不回滚编辑。Stack Overflow是一个问答网站,而不是传统的web论坛。如果您还有其他问题,请随时提问!但是,在旧问题的基础上添加新问题是无效的,特别是如果您已经接受了答案。相反,当你有新问题时,按顶部的“提问”按钮。
append()
方法现在只接受一个参数。(过去需要更多)谢谢,错过了那个!考虑到这一点嗨,这很有效!然而,通过解决这个问题,第二个问题出现了。由于浮点数中的负数,我很难将列表项转换为整数。见上面编辑的问题。再次感谢您的帮助!
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
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'}