用python连接连续的行

2024-05-13 03:10:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个超过30列的python输出文件,输出文件格式是每行用反斜杠分隔成多行。下面是如何将两行分隔为6行的示例:(假设文件名为:EPA.csv)

1 U.S Air Quality\

1 is being monitored\

1 by EPA.

2 And EPA conducts extensive\

2 research on \

2 the ambient air quality.

我想用反斜杠连接连续的行,并且每行只保留一个行号。例如,我希望输出如下所示:

1 U.S. Air Quality is being monitored by EPA.

2 And EPA conducts extensive research on the ambient air quality.

任何帮助都将不胜感激


Tags: andthebyisonairquality斜杠
1条回答
网友
1楼 · 发布于 2024-05-13 03:10:23
>>> t = """1 U.S Air Quality\\
... 1 is being monitored\\
... 1 by EPA.
... 2 And EPA conducts extensive\\
... 2 research on \\
... 2 the ambient air quality."""
>>> d = {}
>>> for i in t.split('\n'):
...  d[i.split(' ')[0]] = i[2:]
>>> d
{'1': 'U.S Air Quality1 is being monitored1 by EPA.', '2': 'And EPA conducts extensive2 research on 2 the ambient air quality.'}
>>> for e in d:
...  print(e, d[e].rstrip('\\'))
... 
1 U.S Air Quality1 is being monitored1 by EPA.
2 And EPA conducts extensive2 research on 2 the ambient air quality.

相关问题 更多 >