![](/img/trans.png)
[英]How to search for a 'text' or 'number' in a csv file with Python AND if exists print only first and second column values to a new csv file
[英]Change position of number in text file - first to second second to first in Python?
嗨,我有一个问题,我想更改数字位置。 第一个数字将是第二个数字,第二个将是第一个,第三个将是第三个等等。这是我输入的一小部分
1 20 135 154 269 288 403 422 537 556 671 690 805 824 939
958 1073 1092 1207 1226 1341 1360 1475 1494 1609 1628 1743 1762 1877 1896
2011 2030 2145 2164 2279 2298 2413 2432 2547 2566 2681 2700 2815 2834 2949
2968 3083 3102 3217 3236 3351 3370 3485 3504 3619 3638 3753 3772 3887 3906
4021 4040 4155 4174 4289 4308 4423 4442 4557 4576 4691 4710 4825 4844 4959
所以预期的 output 将是
20 1 154 135 288 269 etc
但问题是,当我尝试将我的号码放入列表时,我有类似双空格或 \n 等的东西,我写了这个
my_file = open("PN_input.txt", "r")
content = my_file.read()
content_list = content.split(" ")
my_file.close()
print(content_list)
我得到了类似的东西,所以我无法将 go 转到下一步并通过使用例如 for 循环来更改 position
['', '', '', '1', '', '', '20', '', '135', '', '154', '', '269', '', '288', '', '403', '', '422', '', '537', '', '556', '', '671', '', '690', '', '805', '', '824', '', '939\n', '958',
作为我玩 pandas 的一部分......
import pandas as pd
from io import StringIO
data = '''\
1 20 135 154 269 288 403 422 537 556 671 690 805 824 939
958 1073 1092 1207 1226 1341 1360 1475 1494 1609 1628 1743 1762 1877 1896
2011 2030 2145 2164 2279 2298 2413 2432 2547 2566 2681 2700 2815 2834 2949
2968 3083 3102 3217 3236 3351 3370 3485 3504 3619 3638 3753 3772 3887 3906
4021 4040 4155 4174 4289 4308 4423 4442 4557 4576 4691 4710 4825 4844 4959
'''
cols = 'abcdefghijklmno'
out = list('badcfehgjilknmo')
widths = [4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5]
buff = StringIO()
for w, c in zip(widths, cols):
buff.write('%*s' % (w, c))
buff.write('\n')
buff.write(data)
buff.seek(0)
df = pd.read_fwf(buff, widths=widths)
swapped = df[out]
buff = StringIO()
swapped.to_string(buff, header=False, index=False)
print(buff.getvalue())
打印出来
20 1 154 135 288 269 422 403 556 537 690 671 824 805 939
1073 958 1207 1092 1341 1226 1475 1360 1609 1494 1743 1628 1877 1762 1896
2030 2011 2164 2145 2298 2279 2432 2413 2566 2547 2700 2681 2834 2815 2949
3083 2968 3217 3102 3351 3236 3485 3370 3619 3504 3753 3638 3887 3772 3906
4040 4021 4174 4155 4308 4289 4442 4423 4576 4557 4710 4691 4844 4825 4959
这里有一些让你开始的东西:
s = """\
1 20 135 154 269 288 403 422 537 556 671 690 805 824 939
958 1073 1092 1207 1226 1341 1360 1475 1494 1609 1628 1743 1762 1877 1896
2011 2030 2145 2164 2279 2298 2413 2432 2547 2566 2681 2700 2815 2834 2949
2968 3083 3102 3217 3236 3351 3370 3485 3504 3619 3638 3753 3772 3887 3906
4021 4040 4155 4174 4289 4308 4423 4442 4557 4576 4691 4710 4825 4844 4959
"""
lines = (line.split() for line in s.splitlines())
new_lines = [[line[1], line[0], *line[2:]] for line in lines]
for line in new_lines:
print(line)
# prints:
# ['20', '1', '135', '154', '269', '288', '403', '422', '537', '556', '671', '690', '805', '824', '939']
# ['1073', '958', '1092', '1207', '1226', '1341', '1360', '1475', '1494', '1609', '1628', '1743', '1762', '1877', '1896']
# ['2030', '2011', '2145', '2164', '2279', '2298', '2413', '2432', '2547', '2566', '2681', '2700', '2815', '2834', '2949']
# ['3083', '2968', '3102', '3217', '3236', '3351', '3370', '3485', '3504', '3619', '3638', '3753', '3772', '3887', '3906']
# ['4040', '4021', '4155', '4174', '4289', '4308', '4423', '4442', '4557', '4576', '4691', '4710', '4825', '4844', '4959']
一种选择是清理列表以便只保留数字条目,并清理像'939\n'这样的条目:
my_file = open("PN_input.txt", "r")
content = my_file.read()
content_list = content.split(" ")
my_file.close()
new_content_list = []
non_allowed_chars = (' ', '\n', '\t')
for e in content_list:
new_string = e
for character in non_allowed_chars: # replace non-allowed chars for empty string
new_string = new_string.replace(character, "")
if new_string.isalnum(): # checks if string is alphanumeric
new_content_list.append(new_string)
print(new_content_list)
作为旁注,您应该使用with命令打开文件,以便让 python 处理文件的打开和关闭:
with open("PN_input.txt", "r") as my_file:
content = my_file.read()
content_list = content.split(" ")
我这样做了。 这是我的脚本。 如果您对如何更好地编写它有一些建议,请发表评论。 感谢大家的建议。
my_file = open("PN_input.txt", "r")
content = my_file.read()
content_list = content.split()
print(len(content_list))
for i in range(0,400,2):
var1 = content_list[i]
var2 = content_list[i+1]
content_list[i] = var2
content_list[i+1] = var1
print(var1)
print(var2)
print(content_list[i])
print(content_list[i+1])
if i == 400:
break;
print(content_list)
my_file.close()
with open('out.txt', 'w') as f:
for item in content_list:
f.write("%s " % item)
f.close
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.