简体   繁体   English

阅读文本文件时如何删除多个空格

[英]How to remove more than one space when reading text file

Problem: I cannot seem to parse the information in a text file because python reads it as a full string not individual separate strings.问题:我似乎无法解析文本文件中的信息,因为 python 将其作为完整字符串而不是单独的单独字符串读取。 The spaces between each variable is not a \\t which is why it does not separate.每个变量之间的空格不是 \\t 这就是它不分开的原因。 Is there a way for python to flexibly remove the spaces and put a comma or \\t instead? python有没有办法灵活地删除空格并用逗号或\\t代替?

Example DATA:示例数据:

MOR125-1   MOR129-1   0.587
MOR125-1   MOR129-3   0.598
MOR129-1   MOR129-3   0.115

The code I am using:我正在使用的代码:

with open("Distance_Data_No_Bootstrap_RAW.txt","rb") as f:
reader = csv.reader(f,delimiter="\t")
d=list(reader)
for i in range(3):
    print d[i]

Output: ['MOR125-1 MOR129-1 0.587'] ['MOR125-1 MOR129-3 0.598'] ['MOR129-1 MOR129-3 0.115']输出: ['MOR125-1 MOR129-1 0.587'] ['MOR125-1 MOR129-3 0.598'] ['MOR129-1 MOR129-3 0.115']

Desired Output:期望输出:

['MOR125-1', 'MOR129-1', '0.587']
['MOR125-1', 'MOR129-3', '0.598']
['MOR129-1', 'MOR129-3', '0.115']

You can simply declare the delimiter to be a space, and ask csv to skip initial spaces after a delimiter.您可以简单地将分隔符声明为空格,并要求 csv 在分隔符之后跳过初始空格。 That way, your separator is in fact the regular expression ' +' , that is one or more spaces.这样,您的分隔符实际上是正则表达式' +' ,即一个或多个空格。

rd = csv.reader(fd, delimiter=' ', skipinitialspace=True)
for row in rd:
    print row
['MOR125-1', 'MOR129-1', '0.587']
['MOR125-1', 'MOR129-3', '0.598']
['MOR129-1', 'MOR129-3', '0.115']

You can instruct csv.reader to use space as delimiter and skip all the extra space:您可以指示csv.reader使用空格作为分隔符并跳过所有额外的空格:

reader = csv.reader(f, delimiter=" ", skipinitialspace=True)

For detailed information about available parameters check Python docs :有关可用参数的详细信息,请查看Python 文档

Dialect.delimiter A one-character string used to separate fields. Dialect.delimiter 用于分隔字段的单字符字符串。 It defaults to ','.它默认为“,”。 Dialect.skipinitialspace When True, whitespace immediately following the delimiter is ignored. Dialect.skipinitialspace 当为 True 时,紧跟在定界符之后的空格将被忽略。 The default is False.默认值为假。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM