|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
输入文档结果是:
43 ['23', '2', '22', '1']
32 ['45', '1', '12', '1']
12 ['32', '1', '2', '1']
11 ['1', '2', '43', '1']
23 ['43', '1']
44 ['3', '1']
22 ['1', '3', '3', '1', '43', '1']
33 ['2', '1']
怎样把他们符号去掉,改成这样:
43 23 2 22 1
32 45 1 12 1
12 32 1 2 1
11 1 2 43 1
23 43 1
44 3 1
22 13 3 1 43 1
33 2 1
如何用python编程?求助大佬!
本帖最后由 ba21 于 2018-3-18 17:27 编辑
更改楼上代码:
- with open(r"1.txt", "r") as f:
- txt = f.read()
- txt = txt.replace('[','').replace(']','').replace(',',' ').replace('\'','')
-
- with open(r"2.txt", "w") as f:
- f.write(txt)
复制代码
- def joinstr(line):
- line = line[:-1] #去掉尾部\n
- line = line.split(" ") #以2个空格做为分割
- str1 = line[0]
- str2 = line[1]
- temp = ''
- for i in str2[1:-1].split(','): # 去掉['2', '1']的[]后'2', '1'取,号分割后的元组
- temp += i.strip()[1:-1] + ' ' # 去掉空格后取 '2' 中间的数字 2
- return str1+' '+temp
- txt = []
- with open(r"1.txt", "r") as f:
- for line in f:
- txt.append(joinstr(line))
-
- with open(r"2.txt", "w") as f:
- for line in txt:
- f.writelines(line+'\n') #尾部加入\n 写入文件
复制代码
|
|