Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.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字符串_Python_Python 3.x - Fatal编程技术网

更改Python字符串

更改Python字符串,python,python-3.x,Python,Python 3.x,我有一根很长的绳子,看起来像下面。这只是其中的一小部分,但模式会重复 我通过请求读取API时获得的原始数据: ... } #VER B 160 20201020 "Test Bolag AB (4117)" 20201223 { #TRANS 3001 {6 "1000050"} -180000 "" "" 0 #TRANS 2611 {6 "1000050"} -45000 "&quo

我有一根很长的绳子,看起来像下面。这只是其中的一小部分,但模式会重复

我通过请求读取API时获得的原始数据:

...
}
#VER B 160 20201020 "Test Bolag AB (4117)" 20201223
{
#TRANS 3001 {6 "1000050"} -180000 "" "" 0
#TRANS 2611 {6 "1000050"} -45000 "" "" 0
#TRANS 1510 {6 "1000050"} 225000 "" "" 0
}
#VER A 2 20200212 "Test Bolag AB1" 20201223
{
#TRANS 1930 {} -7549 "" "" 0
#TRANS 2641 {} 1209.75 "" "" 0
#TRANS 7990 {} 6339.25 "" "" 0
}
...
我现在写的代码是:

lst = r.text.split('}\r\n')

for i in range(len(lst)):
    tmpstr1 = str(lst[i])
    tmpstr2 = tmpstr1.replace(" ",";")
    tmpstr3 = tmpstr2.replace("\\r","")
    tmpstr4 = tmpstr3.replace("\\n","")
    tmpstr5 = re.sub(r'[^A-Za-z0-9;,#-.]', '', tmpstr4)
    tmpstr6 = tmpstr5.replace("#VER;","")
    tmplst1 = tmpstr6.split('#TRANS;')
    tmpstr7 = str(tmplst1)
    tmpstr8 = str(tmplst1[0])
    tmpstr9 = tmpstr7.replace(";0",tmpstr8)
    tmpstr10 = re.sub(r'[^A-Za-z0-9;,-]', '', tmpstr9)
    tmpstr11 = tmpstr10.strip()

    tmplst2 = tmpstr11.split(',')
    tmplst2.pop(0)
    lst[i] = str(tmplst2)

print(lst[200])
这就是我现在得到的:

['3001;6;1000050;-180000;;B;160;20201020;Kundfaktura;Test;Bolag;AB;4117;20201223', '2611;6;1000050;-45000;;B;160;20201020;Kundfaktura;Test;Bolag;AB;4117;20201223', '1510;6;1000050;225000;;B;160;20201020;Kundfaktura;Test;Bolag;AB;4117;20201223']
这就是我想要的:

3001;6;1000050;-180000;;B;160;20201020;Kundfaktura;Test Bolag AB;4117;20201223 
2611;6;1000050;-45000;;B;160;20201020;Kundfaktura;Test Bolag AB;4117;20201223 
1510;6;1000050;225000;;B;160;20201020;Kundfaktura;Test Bolag AB;4117;20201223

提前谢谢

只需将它们保存在数组的数组中即可。 因此,您将获得数组中的每个条目,这将使它更具动态性,以供将来使用。
为此,请定义一个数组并在每个For循环后追加。

您没有提到您面临的问题。社区中的人如何帮助你。你正在打印字符串列表。如果要将它们合并以创建一个字符串,请使用
print('\n'.join(lst))
@iamimran,我提出的解决方案无法得到我想要的结果。所以我想要的更多的是一个正确方向上的指导,你希望得到我想要的结果。你能给我一个例子吗?你能发布你的文本,这样我就可以尝试你的代码并对其进行优化。r.text是14000行,但在我文章的第一个“代码框”中是它的一小部分,它的结构是一样的,但数字不同。