[英]Python: blank lines added after splitting strings from text file
I'm new to coding and python.我是编码和 python 的新手。 I have text files with journal citations and I need to extract the journal issue, number (if applicable), and either the page numbers or article number (if there are no page numbers).
我有包含期刊引文的文本文件,我需要提取期刊期刊号、编号(如果适用)以及页码或文章编号(如果没有页码)。 I then want to write that information to separate lines in another text file.
然后我想将该信息写入另一个文本文件中的单独行。 For example (target extracts in bold):
例如(目标摘录以粗体显示):
J Antimicrob Chemother. J Antimicrob Chemother。 2012 Dec;
2012 年 12 月; 67(12):2843-7 .
67(12):2843-7 。 doi: 10.1093/jac/dks319.
doi:10.1093/jac/dks319。 Epub 2012 Aug 14.
电子版 2012 年 8 月 14 日。
PLoS One.公共科学图书馆一。 2015 Jun 17;
2015 年 6 月 17 日; 10(6):e0128773 .
10(6):e0128773 。 doi: 10.1371/journal.pone.0128773.
doi:10.1371/journal.pone.0128773。 eCollection 2015. Erratum in: PLoS One.
eCollection 2015。勘误表:PLoS One。 2015;10(9):e0137697.
2015;10(9):e0137697。 PLoS One.
公共科学图书馆一。 2016;11(2):e0148706.
2016;11(2):e0148706。
The code works well when the input is a single string, but when the input is multiple strings in a text file, the output file contains some anomalies.当输入为单个字符串时,代码运行良好,但当输入为文本文件中的多个字符串时,输出文件包含一些异常。 Some of the sliced strings end with a '.'
一些切片字符串以 '.' 结尾。 and a new blank line is inserted after these strings.
并在这些字符串之后插入一个新的空行。
Example input:示例输入:
Trends Microbiol. 1997 Jul;5(7):268-71. Review.
Mol Microbiol. 1996 Sep:21(6):1117-23. Review.
Mol Microbiol. 1996 Aug;21(4):675-82. Review.
Res Vet Sci. 1996 Mar;60(2):168-72.
J Thero Biol. 1994 Jun 7;168(3):281-9.
J Biol Chem. 1993 Aug 25;268(24):18321-9.
J Bacteriol. 1993 May;175(10):3051-7.
Appl Environ Microbiol. 1988 Oct;54(10):2365-70.
Infect Immun. 1983 Oct;42(1):276-84.
Example output:示例输出:
5.7.268-71
21.6.1117-23
21.4.675-82
60.2.168-72.
168.3.281-9.
268.24.18321-9.
175.10.3051-7.
54.20.2365-70.
42.1.276-84
I have tried to correct this by slicing the string again if it ends with a '.'如果字符串以 '.' 结尾,我试图通过再次切片来纠正这个问题。 or ' ', but for some reason this only works with single strings.
或 ' ',但由于某种原因,这只适用于单个字符串。 I would appreciate any suggestions you have as to what I am doing wrong here.
如果您对我在这里做错了什么提出任何建议,我将不胜感激。
slice_words = ['. doi', 'Epub', 'No abstract available', 'Erratum', 'Review', '. eCollection']
char_cleanup = ['). pii: ', ':', ').']
def string_processing(string):
string = string[string.index(";")+1:]
for i in slice_words:
if i in string:
string = string[:string.index(i)]
for j in char_cleanup:
if j in string:
string = string.replace(j, '.')
string = string.replace('(', '.')
while string.endswith('.') or string.endswith(' '):
string = string[:-1]
return string
f = open("reference_strings_OUT.txt", 'w')
with open("reference_strings_IN.txt") as g:
for line in g:
string = line
f.write(string_processing(string))
f.write("\n")
f.close()
This should work:这应该有效:
string = 'J Antimicrob Chemother. 2012 Dec;67(12):2843-7. doi: 10.1093/jac/dks319. Epub 2012 Aug 14'
subString = string[string.index(";")+1::]
subString = subString[0:subString.index(".")]
Let me know how you get on.让我知道你是怎么办的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.