在字符串中添加双引号会给我不正确的数据,在Python中
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在字符串中添加双引号会给我不正确的数据,在Python中相关的知识,希望对你有一定的参考价值。
我正在尝试在文件的每一行中添加双引号,在文件输出中(我想要.tsv输出文件),我在字符串周围得到了四个双引号,当我将其写入时,它给了我正确的结果。 csv文件格式。代码如下
import re
import pandas as pd
df = pd.read_csv('C:/Users/name/Documents/TA/sample.tsv',delimiter=' ',encoding='utf-8')
re_vin = re.compile(r'^.*s')
vin_quotes = []
with open('C:/Users/name/Documents/TA/sample.tsv') as f:
for line in f:
line = line.rstrip('
')
line_quotes = '"{}"'.format(line)
vin_quotes.append(line_quotes)
vin_df = pd.DataFrame(data = vin_quotes[1:])
vin_df.to_csv('C:/Users/name/Documents/TA/processed.tsv', sep=' ', encoding='utf-8',index= False)
示例数据如下
**cvdt35 Output from code**
1GADP5B """1GADP5B"""
1GADP5G """1GADP5G"""
1GAHP2G """1GAHP2G"""
1GM5K8D """1GM5K8D"""
1GM5K8H """1GM5K8H"""
1GMCU0G """1GMCU0G"""
1GMCU9G """1GMCU9G"""
1GMJK1J """1GMJK1J"""
1GTEW1E """1GTEW1E"""
2GMPK4A """2GMPK4A"""
3GA6P0H """3GA6P0H"""
3GA6P0L """3GA6P0L"""
3GA6P0L """3GA6P0L"""
3GAHP0H """3GAHP0H"""
期望的输出
"1GADP5B","1GADP5G","1GAHP2G","1GM5K8D","1GM5K8H","1GMCU0G","1GMCU9G","1GMJK1J","1GTEW1E","2GMPK4A","3GA6P0H","3GA6P0L","3GA6P0L","3GAHP0H"
提前谢谢您
答案
您可以使用以下内容简单地匹配不是"
或空白字符s
的任何字符一次或多次,然后将结果结合在一起。
import re
s = '1GADP5B """1GADP5B"""
1GADP5G """1GADP5G"""
1GAHP2G """1GAHP2G"""
1GM5K8D """1GM5K8D"""
1GM5K8H """1GM5K8H"""
1GMCU0G """1GMCU0G"""
1GMCU9G """1GMCU9G"""
1GMJK1J """1GMJK1J"""
1GTEW1E """1GTEW1E"""
2GMPK4A """2GMPK4A"""
3GA6P0H """3GA6P0H"""
3GA6P0L """3GA6P0L"""
3GA6P0L """3GA6P0L"""
3GAHP0H """3GAHP0H"""'
r = re.findall(r'[^s"]+', s)
r = ",".join(['"{0}"'.format(x) for x in r])
print(r)
输出以下内容:
"1GADP5B","1GADP5B","1GADP5G","1GADP5G","1GAHP2G","1GAHP2G","1GM5K8D","1GM5K8D","1GM5K8H","1GM5K8H","1GMCU0G","1GMCU0G","1GMCU9G","1GMCU9G","1GMJK1J","1GMJK1J","1GTEW1E","1GTEW1E","2GMPK4A","2GMPK4A","3GA6P0H","3GA6P0H","3GA6P0L","3GA6P0L","3GA6P0L","3GA6P0L","3GAHP0H","3GAHP0H"
以上是关于在字符串中添加双引号会给我不正确的数据,在Python中的主要内容,如果未能解决你的问题,请参考以下文章