-
Notifications
You must be signed in to change notification settings - Fork 4k
feat(Data Import): custom csv delimiters, UTF-8 BOM handling #26183
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
45eabd3
feat(Data Import): custom delimiters
c662379
Merge branch 'develop' into data_import_delimiter
vmatt d3cfa26
fix: fallback to ',' when delimiter_options not defined
vmatt e69093c
feat: CSV import introduce FILE_ENCODING_OPTIONS constant in file.py,…
123844b
chore(csvutils): update messages
akhilnarang 1cebdb2
chore(doctype/file): update comments
akhilnarang 0f4e916
fix: comments
9986ea8
chore: update labels and a comment
akhilnarang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
frappe/core/doctype/data_import/fixtures/sample_import_file_semicolon.csv
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Title ;Description ;Number ;another_number ;ID (Table Field 1) ;Child Title (Table Field 1) ;Child Description (Table Field 1) ;Child 2 Title (Table Field 2) ;Child 2 Date (Table Field 2) ;Child 2 Number (Table Field 2) ;Child Title (Table Field 1 Again) ;Child Date (Table Field 1 Again) ;Child Number (Table Field 1 Again) ;table_field_1_again.child_another_number | ||
Test 5 ;test description ;1 ;2 ;"" ; ;"child description with ,comma and" ;child title ;14-08-2019 ;4 ;child title again ;22-09-2020 ;5 ; 7 | ||
; ; ; ; ;child title 2 ;child description 2 ;title child ;30-10-2019 ;5 ; ;22-09-2021 ; ; | ||
;test description 2 ;1 ;2 ; ;child mandatory title ; ;title child man ; ; ;child mandatory again ; ; ; | ||
Test 4 ;test description 3 ;4 ;5 ;"" ;child title asdf ;child description asdf ;child title asdf adsf ;15-08-2019 ;6 ;child title again asdf ;22-09-2022 ;9 ; 71 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
# License: MIT. See LICENSE | ||
import csv | ||
import json | ||
from csv import Sniffer | ||
from io import StringIO | ||
|
||
import requests | ||
|
@@ -39,7 +40,7 @@ def read_csv_content_from_attached_file(doc): | |
def read_csv_content(fcontent): | ||
if not isinstance(fcontent, str): | ||
decoded = False | ||
for encoding in ["utf-8", "windows-1250", "windows-1252"]: | ||
for encoding in ["utf-8-sig", "utf-8", "windows-1250", "windows-1252"]: | ||
akhilnarang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
try: | ||
fcontent = str(fcontent, encoding) | ||
decoded = True | ||
|
@@ -49,15 +50,33 @@ def read_csv_content(fcontent): | |
|
||
if not decoded: | ||
frappe.msgprint( | ||
_("Unknown file encoding. Tried utf-8, windows-1250, windows-1252."), raise_exception=True | ||
_("Unknown file encoding. Tried utf-8-sig, utf-8, windows-1250, windows-1252."), | ||
akhilnarang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
raise_exception=True, | ||
) | ||
|
||
fcontent = fcontent.encode("utf-8") | ||
content = [frappe.safe_decode(line) for line in fcontent.splitlines(True)] | ||
|
||
sniffer = Sniffer() | ||
# Don't need to use whole csv, if more than 20 rows, use just first 20 | ||
sample_content = content[:20] if len(content) > 20 else content | ||
# only testing for most common delimiter types, this later can be extended | ||
# init default dialect, to avoid lint errors | ||
dialect = csv.get_dialect("excel") | ||
try: | ||
# csv by default uses excel dialect, which is not always correct | ||
dialect = sniffer.sniff(sample="\n".join(sample_content), delimiters=frappe.flags.delimiter_options) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might revert this part, the sniffed dialect seems to not work sometimes. Default excel dialect is better - perhaps we could give the user an option. |
||
except csv.Error: | ||
# if sniff fails, show alert on user interface. Fall back to use default dialect (excel) | ||
frappe.msgprint( | ||
_("Delimiter detection failed. Try enable Custom delimiters and adjust Delimiter options."), | ||
indicator="orange", | ||
alert=True, | ||
) | ||
|
||
try: | ||
rows = [] | ||
for row in csv.reader(content): | ||
for row in csv.reader(content, dialect=dialect): | ||
r = [] | ||
for val in row: | ||
# decode everything | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.