- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
最终编辑:有效!感谢大家的帮助,特别感谢 Padraic 在我开始工作之前帮助我。
首先,如果之前有人问过这个问题,我深表歉意,我确实进行了相当广泛的搜索,但也许它的措辞方式出乎我的意料。
所以我正在处理一个像这样的 csv 文件:
0,3,"Braund, Mr. Owen Harris",male,22,1,0,A/5,21171,7.25,S
我必须解析这个文件,然后将它的一部分写入我用这段代码完成的另一个 csv:
import csv
infile = open('data/data.csv', 'r')
incsv = csv.reader(infile, delimiter = ',')
outfile = open('data/output.csv', 'w', newline = '')
outcsv = csv.writer(outfile, delimiter = ',')
问题是“名称”字段的格式为 "Lastname, othernames"
,我需要将其拆分为两个字段:“lastname”和“othernames”。
我似乎无法找到一种方法让它忽略引号并用定界符 (',') 分隔名称。这是一个列表,所以 .strip() 不起作用,而且我无法弄清楚 quote_none 是否不起作用,或者我只是没有语法。
这可能不言而喻,但我对这一切都很陌生。
编辑:我在使用这些解决方案时遇到了错误,因此我将包含其余代码,希望它能突出显示出错的地方。
import csv
infile = open('data/titanic.csv', 'r')
incsv = csv.reader(infile, delimiter = ',')
outfile = open('data/survivors.csv', 'w', newline = '')
outcsv = csv.writer(outfile, delimiter = ',')
dict ={}
for row in incsv:
survived, pclass, name, sex, age, sibsp, parch, ticket, fare, cabin, embarked = row
if survived == "1":
if name not in dict:
dict[name] = name, pclass, sex, age
names = dict.keys()
sorted_names = sorted(names)
for name in sorted_names:
(name, pclass, sex, age) = dict[name]
rowOutput = (name, pclass, sex, age)
outcsv.writerow(rowOutput)
outfile.close()
infile.close()
所以这会解析原始 csv,通过 survived == '1' 进行过滤,将名称添加到字典中(我知道一旦拆分名称字段我将需要调整它),然后按字母顺序对该字典进行排序。
编辑:这是按要求提供的更多原始文件。很抱歉最初没有包含更多内容。
survived,pclass,name,sex,age,sibsp,parch,ticket,fare,cabin,embarked
0,3,"Braund, Mr. Owen Harris",male,22,1,0,A/5 21171,7.25,,S
1,1,"Cumings, Mrs. John Bradley (Florence Briggs Thayer)",female,38,1,0,PC 17599,71.2833,C85,C
1,3,"Heikkinen, Miss. Laina",female,26,0,0,STON/O2. 3101282,7.925,,S
1,1,"Futrelle, Mrs. Jacques Heath (Lily May Peel)",female,35,1,0,113803,53.1,C123,S
0,3,"Allen, Mr. William Henry",male,35,0,0,373450,8.05,,S
0,3,"Moran, Mr. James",male,,0,0,330877,8.4583,,Q
0,1,"McCarthy, Mr. Timothy J",male,54,0,0,17463,51.8625,E46,S
0,3,"Palsson, Master. Gosta Leonard",male,2,3,1,349909,21.075,,S
1,3,"Johnson, Mrs. Oscar W (Elisabeth Vilhelmina Berg)",female,27,0,2,347742,11.1333,,S
这是 892 行的 10 行(如果不计算标题,则为 891)。
最佳答案
您可以在遍历时修改列表:
for row in incsv:
row[2:2] = row[2].split(',')
outcsv.writerow(row)
关于python - 如何将包含引号的 csv 字段拆分为两个字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37321627/
我有一个数组列表: ArrayList allText = new ArrayList(); 其内容是这样的: [Alabama - Montgomery, Alaska - Juneau, Ariz
我有一个 timestamp 格式的开始和结束时间。我想将它们分成多个时间段,例如 1 小时。 $t1 = strtotime('2010-05-06 12:00:00'); $t2 = strtot
我需要将 span10 分成 3 列,但我无法将它们排列起来。我应该在 span10 中添加一个 span12 还是使用 offset 还是??
我有一个时间序列。我想从早上 8 点到第二天早上 7:59 分成 24 小时的区 block 。我知道如何按日期分组,但我尝试过使用 TimeGroupers 和 DateOffsets 处理这个 8
我收到“街道号码邮政编码城市”形式的地址(作为字符串)。我想要做的是将街道和号码与邮政编码和城市分开。通常你可以按空格分割。但有些街道名称中也有空格,例如:“Emile Van Ermengemlaa
我有一个用户列表。其中一些用户处于第一状态,而其他用户处于第二状态。所以我想要的是将这个列表显示为首先,它按排序顺序显示存在 = 1 的用户,然后按排序顺序显示存在 = 2 的用户。这里的排序是根据用
我感觉我搜索了整个网络,但找不到一种方法将不同高度的 div 很好地划分为 3 列,就像 http://www.ing.nl 上那样 headertekst headerteksttesth
Bootstrap 3 按钮下拉菜单出现问题。你可以在这里看到我的两个例子: http://www.bootply.com/W1dLusilMk http://www.bootply.com/GGBv
我在 php 中执行以下操作 foreach($QuestionAsekd as $k => $v){ $grp_name = $v['NAME']; $groupValues[$gr
我找到了一种用pandas解析html的绝妙方法。我的数据格式有点奇怪(见下文)。我想将这些数据拆分为 2 个单独的数据帧。 注意每个单元格如何由,分隔...是否有任何真正有效的方法来分割所有这些单元
HTML 看起来像这样,但我不允许对其进行更改。我只能编写 CSS 将其变成 2 列。 Povezave www.behance.net www.kiberpipa.org www.o
假设我有以下数据框“A” utilization utilization_billable service 1
我需要将 2 个文本框拉伸(stretch)到 100% 的浏览器宽度,以及一个提交按钮。所有三个都应该在一行中,我试图拉伸(stretch)它但它没有发生......有什么想法吗? 代码: .sea
我是一名优秀的程序员,十分优秀!