[英]Extract specific information from string in a csv file
Little backstory first.先说小故事。 I got a script that downloads and opens a .csv file and reads the content.
我有一个脚本可以下载并打开一个 .csv 文件并读取内容。 The part of the script i got a problem with is below.
我遇到问题的脚本部分如下。
file = Download_Folder + str(attachment)
print (file + " is active")
file = open(file, 'r')
for line in file:
if "WOD" in line:
print(WOD)
The problem I got is that every line is between 500 - 1000 characters and I only want the script to return some of them.我遇到的问题是每一行都在 500 - 1000 个字符之间,我只希望脚本返回其中的一些。 I can't use slicing since the numbers of characters before and after my information is different for every line.
我不能使用切片,因为每行我的信息前后的字符数都不同。 Below is an example line.
下面是一个示例行。
2020-01-15 07:05:59 CET;ORD007_PickListReport;'7000009569 7000009494 7000009867 7000009364 7000009808 7000009260;Finished;WOD0200115070558117;884ee259-f2ba-48c6-8ef8-39fc6e48805b
I want the script to find 3 characters ("WOD") in the line string and print them and the 16 characters after.我希望脚本在行字符串中找到 3 个字符(“WOD”)并打印它们以及后面的 16 个字符。 Then go and scan the next line in a loop.
然后去循环扫描下一行。
Use the string index() method:使用字符串 index() 方法:
for line in file:
try:
index = line.index("WOD", 0, len(line))
print(line[index:(index + 17)])
except ValueError:
continue
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.