读取数据集并将其添加到双引号中

2024-09-30 06:20:03 发布

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

Scott Logistics Corp
Transportation One LLC
Brothers Logistics Inc
Western Express Inc
Dart Advantage Logistics
Western Express Inc
Western Express Inc
Landstar Inway
Circle Logistics Inc

请参见上面的数据集,我想在双引号中添加每个名称,例如(“Scott Logistics Corp”),或者参见下面我想对该数据集执行的操作

"Scott Logistics Corp"
"Transportation One LLC"
"Brothers Logistics Inc"
"Western Express Inc"
"Dart Advantage Logistics"
"Western Express Inc"
"Western Express Inc"
"Landstar Inway"
"Circle Logistics Inc"
....

我将从.txt文件中的dataset-1读取数据,并希望将其更改为dataset-2,每个名称用双引号括起来


Tags: onescottincexpressdartcorplogisticscircle
1条回答
网友
1楼 · 发布于 2024-09-30 06:20:03

如果您想添加双引号,那么纯python就足够了:

with open(r"text_1.txt",'r') as file:
    for line in file:
        line = re.sub(r'\n','',line)
        lines_new = (f'"{line}\"\n')
        with open("text_2.txt", "a") as f1:
            f1.writelines(lines_new)



"Scott Logistics Corp"
"Transportation One LLC"
"Brothers Logistics Inc"
"Western Express Inc"
"Dart Advantage Logistics"
"Western Express Inc"
"Western Express Inc"
"Landstar Inway"
"Circle Logistics Inc"

相关问题 更多 >

    热门问题