为了账号安全,请及时绑定邮箱和手机立即绑定

如何拆分字符串中的两个项目?

如何拆分字符串中的两个项目?

婷婷同学_ 2021-03-09 13:12:48
使用.read()读取文件时,如何一次拆分两个对象?我正在尝试同时分割逗号,"\n"但是当我首先分割逗号时,它将把我的字符串变成一个列表,在该列表中我无法再次分割。这是我要拆分的字符串:'States, Total Score, Critical Reading, Mathematics, Writing, Participation (%)\nWashington,1564,524,532,508,41.2000\nNewHampshire,1554,520,524,510,64.0000\nMassachusetts,1547,512,526,509,72.1000\nOregon,1546,523,524,499,37.1000\nVermont,1546,519,512,506,64.0000\nArizona,1544,519,525,500,22.4000\nConnecticut,1536,509,514,513,71.2000\nAlaska,1524,518,515,491,32.7000\nVirginia,1521,512,512,497,56.0000\nCalifornia,1517,501,516,500,37.5000\nNewJersey,1506,495,514,497,69.0000\nMaryland,1502,501,506,495,56.7000\nNorthCarolina,1485,497,511,477,45.5000\nRhodeIsland,1477,494,495,488,60.8000\nIndiana,1476,494,505,477,52.0000\nFlorida,1473,496,498,479,44.7000\nPennsylvania,1473,492,501,480,62.3000\nNevada,1470,496,501,473,25.9000\nDelaware,1469,493,495,481,59.2000\nTexas,1462,484,505,473,41.5000\nNewYork,1461,484,499,478,59.6000\nHawaii,1458,483,505,470,47.1000\nGeorgia,1453,488,490,475,46.5000\nSouthCarolina,1447,484,495,468,40.7000\nMaine,1389,468,467,454,87.1000\nIowa,1798,603,613,582,2.7000\nMinnesota,1781,594,607,580,6.0000\nWisconsin,1778,595,604,579,3.8000\nMissouri,1768,593,595,580,3.6000\nMichigan,1766,585,605,576,3.8000\nSouthDakota,1766,592,603,571,2.0000\nIllinois,1762,585,600,577,4.6700\nKansas,1752,590,595,567,4.7000\nNebraska,1746,585,593,568,3.9000\nNorthDakota,1733,580,594,559,3.4000\nKentucky,1713,575,575,563,5.0000\nTennessee,1712,576,571,565,6.4000\nColorado,1695,568,572,555,14.1000\nArkansas,1684,566,566,552,3.5000\nOklahoma,1684,569,568,547,3.8000\nWyoming,1683,570,567,546,3.6000\nUtah,1674,568,559,547,4.5000\nMississippi,1666,566,548,552,2.2000\nLouisiana,1652,555,550,547,4.0000\nAlabama,1650,556,550,544,5.4000\nNewMexico,1636,553,549,534,7.1000\nOhio,1609,538,548,522,17.2000\nIdaho,1601,543,541,517,14.6000\nMontana,1593,538,538,517,20.0000\nWest Virginia,1522,515,507,500,13.2000\n'
查看完整描述

3 回答

?
慕的地6264312

TA贡献1817条经验 获得超6个赞

不要一次性读取整个文件,请按行读取,然后拆分:


with open(filepath) as f:

    for line in f:

        print line.strip().split(',')

您也可以先在换行符上分割,然后在逗号处循环并分割:


lines = [line.split(',') for line in somestring.splitlines()]

但是对于逗号分隔的文件,最好的选择是使用csv模块:


import csv


with open(filepath, 'rb') as f:

    reader = csv.reader(f, delimiter=',')

    for row in reader:

        print row

这为您提供了以下行:


['States', ' Total Score', ' Critical Reading', ' Mathematics', ' Writing', ' Participation (%)']

['Washington', '1564', '524', '532', '508', '41.2000']

['NewHampshire', '1554', '520', '524', '510', '64.0000']

由于第一行带有标题,因此您也可以使用aDictReader并获取将标题映射到值的字典:


with open(filepath, 'rb') as f:

    reader = csv.DictReader(f, delimiter=',')

    for row in reader:

        print row

        # address columns as: row['States'], row['Total Score']

将行输出为:


{' Writing': '508', ' Total Score': '1564', ' Critical Reading': '524', 'States': 'Washington', ' Mathematics': '532', ' Participation (%)': '41.2000'}


查看完整回答
反对 回复 2021-03-27
?
明月笑刀无情

TA贡献1828条经验 获得超4个赞

有re.split多个字符拆分:


import re

re.split("\n| ","this is\na short\ntest...")

>>> ['this', 'is', 'a', 'short', 'test...']


查看完整回答
反对 回复 2021-03-27
  • 3 回答
  • 0 关注
  • 126 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信