- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
假设您的输入格式如下:
id____value1____value2...valueN
1____hello____world...something
2________goodnight...world
4个'_'
应该是'/t'
到目前为止,我得到这样的结果:第一项有一个 {ID:1, value1:hello, value2:world,...,valueN:something}
而第二项有{ID:2, value1: , value2:goodnight, ... , valueN: world}
我希望我对第二项的最终表示是:{ID:2, value1:n/a , value2:goodnight, ... , valueN: world}
我已经用 Python 编写了一个脚本来逐行读取文件,但我希望能够检查 '/t'
后面是否跟着另一个 '/t'
,然后插入 'n/a'
值。
到目前为止我的代码是这样的:
def myFunc():
list = []
with open(file, 'r') as f:
header = f.readline() # Store the header of the file for future reference.(maybe). Don't commend out.
for line in f:
for i in range(len(line)):
if line[i] == '\t':
if line[i+1] == '\t':
line[:i] + "n/a" + line[i:]
list.append(line) # iterate through the file and store it's values on the list.
return list
最佳答案
取决于你想在一天结束时如何使用列表,你也可以使用 csv
模块来做一些事情,这对于不止一个的情况会更灵活一些列可能没有条目;
import csv
with open(file, 'r') as f:
reader = csv.reader(f, delimiter='\t')
header = next(reader)
list = [[x if x else 'n/a' for x in line] for line in reader]
现在 list
将是一个列表列表,每个列表都包含实际项目。
In [11]: print(header)
['id', 'value1', 'value2', 'value3']
In [12]: print(list)
[['1', 'hello', 'world', 'something'], ['2', 'n/a', 'goodnight', 'world']]
编辑在下面评论后添加:
对上述方法稍作修改(使用 Python 2.7+ 字典理解)将为您提供字典;
import csv
with open(file, 'r') as f:
reader = csv.reader(f, delimiter='\t')
header = next(reader)
list = [{header[i]: line[i] if line[i] else 'n/a' for i in range(len(header))} for line in reader]
print(list)
# [{'value1': 'hello', 'value3': 'something', 'id': '1', 'value2': 'world'}, {'value1': 'n/a', 'value3': 'world', 'id': '2', 'value2': 'goodnight'}]
你问这是否更干净,这可能在很大程度上取决于你打算如何使用结果。如果您决定检查结果,字典方法会为您提供更易于阅读的内容。
如果您需要对文件执行大量数据处理,您可能会对 pandas
DataFrame
数据结构感兴趣对于这种东西。但是,如果您不在那种情况下,那么这种方法可能完全是矫枉过正。几个简单的例子说明它的作用(例如注意它默认处理你原来的 'n/a'
问题):
In [1]: import pandas as pd
In [5]: df = pd.read_csv('testfile', delimiter='\t') # Or whatever your file is called
In [6]: df = df.set_index('id')
In [7]: df
Out[7]:
value1 value2 value3
id
1 hello world something
2 NaN goodnight world
In [8]: df[df['value3'] == 'something'] # Find all rows with a given value3
Out[8]:
value1 value2 value3
id
1 hello world something
In [10]: df[df['value2'] == 'goodnight'] # Find all rows with a given value2
Out[10]:
value1 value2 value3
id
2 NaN goodnight world
In [11]: df['value1'] # Show only value1
Out[11]:
id
1 hello
2 NaN
Name: value1, dtype: object
基本上,您可以想出的任何表操作在 pandas
中都有一种自然的方法。
关于python - 替换输入 Python 的缺失值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38876862/
我编写了一个 Android 应用程序,它使用 Azure 来执行用户通过 Google、Twitter 和 Facebook 的登录;它使用 Microsoft.WindowsAzure.Mobil
我想将 AdomdClient 引用添加到 C# 项目,但它不在引用列表中。客户端列在程序集文件夹 C:\Windows\Assembly 中。 计算机上安装了 SQL Server 2012。 最佳
我正在学习“绘图应用程序”的教程。当我在 Firefox 上启动我的应用程序时,Firebug 告诉我“在语句之前缺少 ;” 我在第 9 行调用函数的位置。我只是不明白应该将这些“;”放在哪里. va
我想将 AdomdClient 引用添加到 C# 项目,但它不在引用列表中。客户端列在程序集文件夹 C:\Windows\Assembly 中。 计算机上安装了 SQL Server 2012。 最佳
我在 Firebug 中不断收到关于 onClick 事件的错误。 我已经尝试了 "和 ' 的各种不同组合,但无济于事。在添加 onClick 事件之前,这工作正常。 有人能发现我可能做错了什么吗?
Visual Studio 2015 告诉我找不到 WSASetSocketSecurity。 该 dll 存在并且还包括似乎没问题。 我的包括: windows.h stdio.h Wincrypt
我需要访问 eloquent 的 whereHasNot方法(此处添加: https://github.com/laravel/framework/commit/8f0cb08d8ebd157cbfe
跟随宠物物体检测的 TF 教程:https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/run
构建路径 > 添加库 > JUnit 无法添加 JUnit3 或 JUnit4 组件。 我在.log 中看到这样的消息 !MESSAGE No property tester contributes
我正在运行此脚本来查看网络上的摄像机: gst-launch udpsrc port=1234 ! "application/x-rtp, payload=127" ! rtph264depay !
我正在使用http://java.sun.com/jsp/jstl/fmt用于从 Spring 配置中设置的 Message Resource Bundle 输出消息的标签库。消息解析也可以放在 Co
我正在将 Ninject 与 MVC4 连接起来,并让它工作到尝试实际解决依赖关系的程度。但是,我收到以下异常: Method not found: 'System.Web.Http.Services
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎与 help center 中定义的范围内的编程无关。 . 关闭 9 年前。 Improve
我在启动 ASP.NET MVC5 应用程序时遇到问题。到目前为止一切正常。启动应用程序时出现以下错误: Could not load file or assembly 'Microsoft.Appl
我已经使用以下方法创建了一个环境: conda create --prefix C:\Users\Dell\Dropbox\DjangoProjects\webenv python=3.6 执行后:c
我们有一个遗留的 Web 窗体应用程序,我们最近将其从网站项目转换为 Web 应用程序项目。 Web 窗体项目是解决方案的“启动”项目。 有一个 MVC 项目是对 Web 窗体项目的引用。 在 MVC
使用某种字体,我使用Java的FontLayout来确定它的上升、下降和行距。 (参见 Java 的 FontLayout 教程 here) 在我的具体案例中,我使用的是 Arial Unicode
我正在尝试在 linux 下编译 qt ffmpeg 包装器简单编码/解码示例 QTFFmpegWrapper source # Set list of required FFmpeg librari
我正在使用来自开发人员 android 页面的 SlidingTabLayout.java。在我使用 slidingTabLayout.setDistributeEvenly(true); 使 sli
我正在尝试使用 v360 filter 将 180° 鱼眼视频转换为普通/常规视频的 FFmpeg . 这是我尝试过的命令:ffmpeg -i in.mp4 -vf "v360=input=fishe
我是一名优秀的程序员,十分优秀!