Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用Numpy保存文件时如何保留哈希标记符号?_Python_Pandas_Numpy_Csv_Data Cleaning - Fatal编程技术网

Python 使用Numpy保存文件时如何保留哈希标记符号?

Python 使用Numpy保存文件时如何保留哈希标记符号?,python,pandas,numpy,csv,data-cleaning,Python,Pandas,Numpy,Csv,Data Cleaning,我正在清除不需要的字符串中的一些文本数据。我的文本数据在第一行包含#,当我保存文件时,它将消失,因为Python无法读取它 文本示例: @peak,+ID,#val 1,nopeak 2,nopeak 3,peak 4,nopeak @category,+ID,#val 1,high 2,low 3,high 4,high 我如何删除行中不需要的字符串: data1 = np.genfromtxt('text.b', dtype=str, delimiter="\t") idxList =

我正在清除不需要的字符串中的一些文本数据。我的文本数据在第一行包含#,当我保存文件时,它将消失,因为Python无法读取它

文本示例:

@peak,+ID,#val
1,nopeak
2,nopeak
3,peak
4,nopeak

@category,+ID,#val
1,high
2,low
3,high
4,high
我如何删除行中不需要的字符串:

data1 = np.genfromtxt('text.b', dtype=str, delimiter="\t")

idxList = [1,2]

for p,q in enumerate(idxList):
    OutArr1 = []
    RemoveStr = str(q)
    for i,j in enumerate(data1):
        if j[:4] != RemoveStr: OutArr1.append(str(j))   
    OutArr2 = np.asarray(OutArr1, dtype=np.str)
    np.savetxt('sample_' + str(q) + '.txt', OutArr2, fmt='%s')
电流输出:

@peak,+ID,
3,peak
4,nopeak

@category,+ID,
3,high
4,high
如何在每一行保留哈希标记符号(#val?

“#”用作注释起始符,默认配置为
genfromtxt
。简单地替换为:

data1 = np.genfromtxt('text.b', dtype=str, delimiter="\t", comments=None)

这被解释为注释标记。请检查
comments
参数以了解为什么要为此使用numpy?你能分享更多的程序和数据吗?这可能是csv模块或熊猫的作业。此外,变量和函数名应遵循带有下划线的
小写形式。