使用字段和文本分隔符拆分文本

时间:2018-03-31 13:06:18

标签: python linux shell scripting grep

示例行:

"1","Point 1","value1,value2,value3","value1: funtion1**\n**value2: function2","UUID=12345","description"

我想提取第1列,第2列和第5列。

  • 我不能使用逗号,因为col3有多个逗号。
  • 第4列包含多个新行\n,因此无法访问第5列(UUID)。

我们可以通过使用excel打开一个csv文件来实现这一点,只需要选择字段分隔符和文本分隔符。是否可以使用脚本编写?

您可以使用this link下载文件。

3 个答案:

答案 0 :(得分:0)

如果示例行是字符串。以下片段应该有所帮助。

s = """"1","Point 1","value1,value2,value3","value1: funtion1**\n**value2: function2","UUID=12345","description"
I """

val = [i.strip('"') for i in s.split('","')]
print(val)
print(val[4])     #use index to get element

<强>输出:

['1', 'Point 1', 'value1,value2,value3', 'value1: funtion1**\n**value2: function2', 'UUID=12345', 'description"\nI ']
UUID=12345

答案 1 :(得分:0)

注意:此解决方案很简单,但高度依赖于格式一致。 假设引号和逗号之间没有间距,您可以将它们分隔为","

#input string
s = '"1","Point 1","value1,value2,value3","value1: funtion1**\n**value2: function2","UUID=12345","description"' 

# remove quotation marks and whitespace from edges, then split string into tuple
cols = s.strip('" ').split('","') 

现在您可以提取列,例如第5列为cols[4]

>> cols[4]
'UUID=12345'     

答案 2 :(得分:0)

如果我理解你想做什么,请使用以下python:

import csv
with open('/tmp/test.csv', 'r') as csvfile:
     reader = csv.reader(csvfile, delimiter=',', quotechar='"')
     for row in reader:
         for i, cell in enumerate(row):
            print("Cell %d: %s" % (i, cell))

无论是否包含分隔符或新行,您都可以获取每列。输出:

Cell 0: 1
Cell 1: Point 1
Cell 2: value1,value2,value3
Cell 3: value1: funtion1**\n**value2: function2
Cell 4: UUID=12345
Cell 5: description

现在,我打开你的链接xlsx并用libreOffice保存为csv,代码处理新行:

Cell 0: 1
Cell 1: point1
Cell 2: value1, value2,value3
Cell 3: Line1

Line2.

Line3.
Cell 4: UUID=123545
time=123seconds

Start time: x

End time: y
Cell 5: 1234

原始csv内容是(请注意,尽管它们代表单个,但实际上它们实际上跨文件中的多个):

1,point1,"value1, value2,value3","Line1

Line2.

Line3.","UUID=123545
time=123seconds

Start time: x

End time: y",1234

如果上述代码段可以处理您的数据,但是如果它们确实是CSV格式,请告诉我,它应该能够正确读取单元格