簡體   English   中英

如何從 Python 中的數據中刪除多余的逗號

[英]How to remove extra commas from data in Python

我有一個 CSV 文件,我試圖通過該文件將數據加載到包含 2 列的 SQL 表中。 我有 2 列,數據用逗號分隔,用於標識下一個字段。 第二列包含文本和該文本中的一些逗號。 由於額外的逗號,我無法將數據加載到我的 SQL 表中,因為它看起來有額外的列。 我有數百萬行數據。 如何刪除這些多余的逗號?

數據:

Number Address
"12345" , "123 abc street, Unit 345"
"67893" , "567 xyz lane"
"65432" , "789 unit, mno street"

我想刪除隨機行地址中多余的逗號。

如果您的所有數據都采用相同的格式,例如Number Address "000", "000 abc street, Unit 000" ,您可以拆分列表,刪除逗號,然后將列表重新組合在一起,使其再次成為字符串。 例如使用您提供的數據:

ori_addr = "Number Address \"12345\" , \"123 abc street, Unit 345\""
addr = ori_addr.split()
addr[6] = addr[6].replace(",", "")
together_addr = " ".join(addr)

together_addr 等於“編號地址“12345”、“123 abc street Unit 345”注意“street”和“Unit”之間沒有逗號。

編輯:

  • 根據用戶的評論,在此測試中添加了一個失敗的地址。 此地址加載到數據庫沒有問題。
  • 添加了將 CSV 地址存儲到 MySQL 的代碼。

回答:

下面的代碼執行以下操作:

  • MySQL 數據庫engine (連接)已創建。
  • 從 CSV 文件中讀取的地址數據(編號、地址)。
  • 從源數據中替換了非字段分隔逗號,並刪除了額外的空格。
  • 輸入DataFrame的編輯數據
  • DataFrame用於將數據存儲到 MySQL 中。
    import csv
    import pandas as pd
    from sqlalchemy import create_engine

    # Set database credentials.
    creds = {'usr': 'admin',
             'pwd': '1tsaSecr3t',
             'hst': '127.0.0.1',
             'prt': 3306,
             'dbn': 'playground'}
    # MySQL conection string.
    connstr = 'mysql+mysqlconnector://{usr}:{pwd}@{hst}:{prt}/{dbn}'
    # Create sqlalchemy engine for MySQL connection.
    engine = create_engine(connstr.format(**creds))

    # Read addresses from mCSV file.
    text = list(csv.reader(open('comma_test.csv'), skipinitialspace=True))

    # Replace all commas which are not used as field separators.
    # Remove additional whitespace.
    for idx, row in enumerate(text):
        text[idx] = [i.strip().replace(',', '') for i in row]

    # Store data into a DataFrame.
    df = pd.DataFrame(data=text, columns=['number', 'address'])
    # Write DataFrame to MySQL using the engine (connection) created above.
    df.to_sql(name='commatest', con=engine, if_exists='append', index=False)

源文件( comma_test.csv ):

"12345" , "123 abc street, Unit 345"
"10101" , "111 abc street, Unit 111"
"20202" , "222 abc street, Unit 222"
"30303" , "333 abc street, Unit 333"
"40404" , "444 abc street, Unit 444"
"50505" , "abc DR, UNIT# 123 UNIT 123"

未經編輯的數據:

['12345 ', '123 abc street, Unit 345']
['10101 ', '111 abc street, Unit 111']
['20202 ', '222 abc street, Unit 222']
['30303 ', '333 abc street, Unit 333']
['40404 ', '444 abc street, Unit 444']
['50505 ', 'abc DR, UNIT# 123 UNIT 123']

編輯數據:

['12345', '123 abc street Unit 345']
['10101', '111 abc street Unit 111']
['20202', '222 abc street Unit 222']
['30303', '333 abc street Unit 333']
['40404', '444 abc street Unit 444']
['50505', 'abc DR UNIT# 123 UNIT 123']

從MySQL查詢:

number  address
12345   123 abc street Unit 345
10101   111 abc street Unit 111
20202   222 abc street Unit 222
30303   333 abc street Unit 333
40404   444 abc street Unit 444
50505   abc DR UNIT# 123 UNIT 123

致謝:

這是一個冗長的方法。 但是,每個步驟都被有意分解,以清楚地顯示所涉及的步驟。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM